如何自动查找字符串中字符存在的次数

How to automatically find how many times a character existed in a string?

本文关键字:存在 字符 何自动 查找 字符串      更新时间:2023-10-16

我是 c++ 的初学者,我做了一个代码提到字符串中存在字符的次数:-

#include <string>
#include <iostream>
using namespace std;
int main(){
int s=0;
int l=0;
int o=0;
int w=0;
int y=0;
string boo="slowly";
for (size_t j = 0; j < boo.size(); j++) {
while (boo[j] == 's') {
s=s+1;
break;}
while (boo[j] == 'l')
{
l=l+1;
break;}
while (boo[j] == 'o') {
o=o+1;
break;}
while (boo[j] == 'w') {
w=w+1;
break;}
while (boo[j] == 'y') {
y=y+1;
break;}}
cout <<"s ="<<s<<endl;
cout <<"l ="<<l<<endl;
cout <<"o ="<<o<<endl;
cout <<"w ="<<w<<endl;
cout <<"y="<<y<<endl;
system("pause");
return 0;
}

我想知道如何制作一个代码来自动检测字符串中的字符并对其应用条件,而无需为每个字母字符制作 while 循环并为每个字符制作 int 变量?

**请原谅我的坏英语

您可以使用地图来跟踪不同的字符计数。您循环访问一次输入,然后针对每个字符在地图中查找它。如果地图中尚不存在它,则将创建一个值为 0 的新条目。

创建地图后,每个条目

都以字符为键,出现次数作为值,循环访问地图并打印每个条目。

#include <iostream>
#include <map>
#include <string>
int main()
{
    std::string input = "slowly";
    std::map<char, int> occurrences;
    for (char character : input)
    {
        occurrences[character] += 1;
    }
    for (auto& entry : occurrences)
    {
        std::cout << entry.first << '=' << entry.second << std::endl;
    }
}

试试这个:

#include <string>
#include <iostream>
using namespace std;
int main(){
   int s=0,l=0,o=0,w=0,y=0;
   string boo="slowly";
   for (int j = 0; j < boo.size(); j++) {
       switch(boo.charAt(j)){
            case 's':
               s++;
               break;
            case 'l':
               l++;
               break;
            case 'o':
               o++;
               break;
            case 'w':
               w++;
               break;
            case 'y':
               y++;
               break;
       }
   }
   cout <<"s ="<<s<<endl;
   cout <<"l ="<<l<<endl;
   cout <<"o ="<<o<<endl;
   cout <<"w ="<<w<<endl;
   cout <<"y="<<y<<endl;
   system("pause");
   return 0;
}

如果您的编译器支持 C++ 2011,那么您可以按以下方式编写,前提是字母将按照它们在字符串中出现的顺序列出

#include <iostream>
#include <string>
#include <map>
int main()
{
    std::string s( "slowly" );
    auto comp = [&]( char c1, char c2 ) { return s.find( c1 ) < s.find( c2 ); };
    std::map<char, int, decltype( comp )> m( comp );
    for ( char c : s ) ++m[c];
    for ( auto p : m ) std::cout << p.first << ": " << p.second << std::endl;
}    

程序输出为

s: 1
l: 2
o: 1
w: 1
y: 1

否则,如果编译器不支持 C++ 2011,则必须使用在 main 之前定义的函数类而不是 lambda。此外,youi 应该将基于范围的 for 循环替换为普通循环。

使用一个小技巧。首先,观察所有小写字符都按升序使用数字代码编码:

      ASCII EBCDIC
'a' - 97    129
'b' - 98    130
'c' - 99    131
...
'z' - 122   169

代码可能不是连续的,就像 EBCDIC 的情况一样,但这并不重要:创建一个足够大小的int数组,遍历您的字符串,并为每个小写字符ch标记索引ch-'a'。表达式表示字符代码与字母表开头之间的距离。

现在你可以遍历 int s 的数组,并通过执行索引i的逆平移来打印相应的字符:

 char ch = 'a'+i;

下面是一个完整的示例:

string s;
getline(cin, s);
int seen['z'-'a'+1] {0};
for (int i = 0 ; i != s.size() ; i++) {
    char ch = s[i];
    if (!islower(ch))
        continue;
    seen[ch-'a']++;
}
for (int i = 0 ; i != 'z'-'a'+1 ; i++) {
    if (seen[i]) {
        cout << (char)('a'+i) << " - " << seen[i] << endl;
    }
}

演示。

我想STL的算法头会帮助你:

std::string boo="slowly";
std::cout << "s = " << std::count(boo.cbegin(), boo.cend(), 's') << std::endl;