Write a C++ program to find frequency of any character in a string,where string is entered by user.

Program Code:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int i, count=0;
char str[100], ch;
cout<<"Enter the String to find frequency of character in it is : ";
gets(str);
cout<<endl;
cout<<"Enter a character to find its frequency in the given string : ";
cin>>ch;
cout<<endl;
for(i=0; str[i]!='\0'; i++)
{
if(ch==str[i])
{
count++;
}
}
cout<<"Frequency of the entered character in the given string is : ";

cout<<count;
return 0;
}

The program code is tested on www.jdoodle.com
Output:
Enter the String to find frequency of character in it is : MukeshMukeshMukesh
Enter a character to find its frequency in the given string : k
Frequency of the entered character in the given string is : 3


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: