I'm a beginner in c++ and i made a code mentions how many times a character existed in a string :-
#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;
}
I was wondering how to make a code that automatically detect the character in the string and apply the condition on it without making a while loop for every single alphabet character and making int variables for every character ?
**Excuse my bad Englihs