在C++中搜索一系列字符串中的特定字符

发布于 2025-02-13 23:49:18 字数 745 浏览 2 评论 0原文

用户必须输入3个字符串,我们必须将它们放入数组中。 然后,用户输入了他想在输入字符串中找到的字符。 然后,如果找到炭,请更新计数器,以显示出现多少次char。

示例:

用户输入字符串:猫车观看

用户char输入:a

结果:

字母A出现3次!

如何通过字符串搜索这样的数组可以找到特定的炭?

下面的代码,但我卡住了:

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!";

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!";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2025-02-20 23:49:19

而不是手动编写循环,而是使用标准算法,例如std :: count

这是一个演示程序,

#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';
}

程序输出是

count = 3

如果您的编译器支持C ++ 20,那么您可以再次以以下方式编写程序

#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';
}

输出

count = 3

为您的代码,那么此代码段

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

是不正确的。您还需要一个内部循环才能穿越每个字符串,例如

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

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++;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文