0

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!";
7
  • 1
    Well, the standard algorithm would be count. en.cppreference.com/w/cpp/algorithm/count and just concatenate "s0 + s1 + s2" the three strings together before the count. Commented Jul 8, 2022 at 10:55
  • 2
    You are comparing a char to a string. Instead loop through the string to get the individual characters Commented Jul 8, 2022 at 10:55
  • Also, please provide a minimal reproducible example. Something that we can copy/paste into the compiler. I'm too tired of fixing all the #includes and stuff before answering. Commented Jul 8, 2022 at 10:58
  • 1
    You can use std::string::find, but you'll have to call it repeatedly if you want to count every occurrence in each string. std::count is much easier (and ranges::count if you have C++20) Commented Jul 8, 2022 at 10:58
  • 1
    If you want to find out how many times a single character appears in the string, how do you propose to go about doing it? Comparing the single char to an entire string, like this: 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. Commented Jul 8, 2022 at 10:59

1 Answer 1

6

Instead of writing for loops manually it is better to use standard algorithms as for example std::count.

Here is a demonstration program

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

int main()
{
    std::string words[] = { "Cat", "Car", "Watch" };
    char c = 'a';

    size_t count = 0;

    for ( const auto &s : words )
    {
        count += std::count( std::begin( s ), std::end( s ), c );
    }

    std::cout << "count = " << count << '\n';
}

The program output is

count = 3

If your compiler supports C++ 20 then you can write the program the following way

#include <iostream>
#include <string>
#include <iterator>
#include <ranges>
#include <algorithm>

int main()
{
    std::string words[] = { "Cat", "Car", "Watch" };
    char c = 'a';

    size_t count = std::ranges::count( words | std::ranges::views::join, c );

    std::cout << "count = " << count << '\n';
}

Again the program output is

count = 3

As for your code then this code snippet

for (int i = 0; i < 3; i++)
{
    if (userString[i] == userChar)
    {
        counter++;
    }
}

is incorrect. You need one more inner for loop to traverse each string as for example

for (int i = 0; i < 3; i++)
{
    for ( char c : userString[i] )
    {
        if ( c == userChar)
        {
            counter++;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.