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
Post A Comment:
0 comments: