350,928 views
40 votes
40 votes
Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string in c++

User Piyush Dubey
by
2.4k points

1 Answer

17 votes
17 votes
#include
#include

using namespace std;

int CountCharacters(char userChar, string userString){
int result = 0;
for(int i = 0;i if(userString[i] == userChar){
result++;
}
}
return result;
}

int main(){
string userString;
char userChar;
cin>>userChar>>userString;
cout< return 0;
}
User Chrismit
by
2.9k points