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.
The Answer to the Question
is below this banner.
Can't find a solution anywhere?
NEED A FAST ANSWER TO ANY QUESTION OR ASSIGNMENT?
Get the Answers Now!You will get a detailed answer to your question or assignment in the shortest time possible.
Here's the Solution to this Question
#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;
}