codescracker


c++

C++ Program to Find Frequency of Character in String



« Previous Program Next Program »


Find Frequency of Characters in String in C++

To find the frequency of character or count occurrence of character in string in C++ programming, you have to ask to the user to enter the string, again ask to enter the character to find the frequency of that character (or to count the occurrence of character) in given string. After finding the frequency of the character, display the result on the output screen as shown here in the following program.

C++ Programming Code to Find Frequency of Character in String

Following C++ program ask to the user to enter a string and a character to find the frequency of the entered character in the string :

/* C++ Program - Find Frequency of Character in String */
		
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
	clrscr();
	int i, count=0;
	char str[1000], ch;
	cout<<"Enter the String : ";
	gets(str);
	cout<<"Enter a character to find frequency : ";
	cin>>ch;
	for(i=0; str[i]!='\0'; i++)
	{
		if(ch==str[i])
		{
			count++;
		}
	}
	cout<<"Frequency of the character "<<ch<<" = "<<count;
	getch();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program find frequency of character

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test