我在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變量?
**請原諒我糟糕Englihs
的可能的複製[計數字符串中的字符出現](HTTP://計算器。com/questions/3867890/count-character-occurrences-in-a-string) – LogicStuff
No sir我不想要確切的字符我想自動檢測字符串中的每個字符並提及每個字符重複的次數 – Dreamer