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. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char ch;
cout << "Please, enter a character: ";
cin >> ch;
string str;
cout << "Please, enter a string: ";
cin >> str;
int n = 0;
for (int i = 0; i < str.length(); i++)
{
if (str[i] == ch)
n++;
}
cout << "Output: ";
if (n != 1)
cout << n<<"'s";
else
cout << n;
}
Comments
Leave a comment