Answer to Question #305665 in C++ for Program Training

Question #305665

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.


1
Expert's answer
2022-03-03T14:33:49-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog