21.3k views
5 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 CJR
by
4.2k points

1 Answer

4 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 Peralmq
by
4.7k points