How to find the frequency of a character in a string with C++

1 Answer

0 votes
#include <iostream>

int main(void)
{
	std::string s = "c c++ c# java python";
	int frequency = 0;
	char ch = 'c';

	for (int i = 0; s[i]; i++)
	{
		if (ch == s[i])
			frequency++;
	}

	std::cout << "Frequency of the characters '" << ch << "' is : " << frequency << std::endl;

	return(0);
}



/*
run:

Frequency of the characters 'c' is : 3

*/

 



answered Jun 12, 2017 by avibootz

Related questions

1 answer 112 views
1 answer 132 views
1 answer 112 views
1 answer 135 views
1 answer 161 views
...