The user has to enter 3 strings and we have to put them in an array. Then user enters a char that he wants to find in input strings. Then if the char is found, update the counter for how many times char appeared.
Example:
User input string: Cat Car Watch
User char input: a
Result:
Letter a appears 3 times!
How can I search through a string array like this to find specific chars?
Code below but I'm stuck:
string userString[3];
for (int i = 0; i < 3; i++)
{
cout << "Input string: ";
getline(cin, userString[i]);
}
char userChar;
cout << "Input char you want to find in strings: ";
cin >> userChar;
int counter = 0;
for (int i = 0; i < 3; i++)
{
if (userString[i] == userChar)
{
counter++;
}
}
cout << "The char you have entered has appeared " << counter << " times in string array!";
std::string::find, but you'll have to call it repeatedly if you want to count every occurrence in each string.std::countis much easier (andranges::countif you have C++20)userString[i] == userChar? Does that make sense to you? Have you learned about range iteration in C++ yet? What is the topic of the chapter in your textbook where this practice problem is from, or what was the topic in class where this homework assignment was given out? If you can explain in which context this coding assignment was given this will help point to the right approach for doing this.