C++ Program to calculate frequency of a character in a given string [DEVCPP/GCC]

FREQUENCY

Frequency of a character in a given string implies no of occurences of that particular character in the given string.

For example:

String:        Techcpp
Character:  c
Frequency: 2


STEPS

1. Initialize count with 0.

2. Input an string and the character whose frequency is to be calculated within the entered string.

3. Initialize index variable i with 0

4. Increment the count if the condition 'ch equals to a[i]' is satisfied.

5. Increment i untill a[i]!='\0'. 

PROGRAM

//Program to calculate the frequency of a character

# include <iostream>

using namespace std;

int main()
{
char a[20],ch;
int i,j,count=0;

cout<<"ENTER THE STRING:\n";
cin.getline(a,20);

cout<<"ENTER THE CHARACTER WHOSE FREQUENCY IS TO BE CALCULATED:\n";
cin>>ch;

for(i=0;a[i]!='\0';i++)
{
if(ch==a[i])
count++;
}

cout<<count;
return 0;
}

OUTPUT

C++ Program to calculate frequency of a character in a given string with output

Share this

Related Posts

FIND US ON FACEBOOK!