Answer the question
In order to leave comments, you need to log in
How to iterate over a char array through for letter by letter?
I need to compare each letter in an array
#include <iostream>
using namespace std;
int countAccurance(char s1, char s2, char string1, char string2) {
int size1 = sizeof(string);
int size2 = sizeof(string);
int as1 = 0;
int as2 = 0;
for (int i = 0; i < size1; i++) {
if (string1[i] == s1) {
as1++;
}
}
for (int i = 0; i < size2; i++) {
if (string2[i] == s1) {
as2++;
}
}
}
int main() {
char s1, s2;
cin >> s1 >> s2;
char string[20];
char string2[20];
gets_s(string);
gets_s(string2);
}
Answer the question
In order to leave comments, you need to log in
1. Do not use type arrays тип arr[размер];
(raw arrays). Otherwise, pain and suffering. Array = vector<int>
(Well, or there double
, or whatever. You can easily google the tutorial on vector.)
2. To store strings in C ++ is used string
(needed #include <string>
)
3. sizeof
generally about something else, and a beginner cannot need it. Instead: string.size()
4. Actually, the answer to the question "iterate over the characters of a string":
for (const char c : str) {
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question