H
H
Helcurt2020-10-13 14:45:33
C++ / C#
Helcurt, 2020-10-13 14:45:33

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);
}


and I wanted to iterate through the array (string) through the string but it doesn't work

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Zhilin, 2020-10-13
@Anton3

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. sizeofgenerally 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) {
  ...
}

Where instead of str substitute the desired variable name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question