fastread homefastrread library fastread menu

C++ : Find the Frequency of Characters in a String

Tutorial by:Manisha Dubey      Date: 2016-06-18 04:58:19

❰ Previous Next ❱

This program asks user to enter a string and a character and this program checks how many times that character is repeated in the string entered by user.

Source Code to Find the Frequency of Characters


#include <iostream>
#include <cstring>
using namespace std;
int main(){
   char c[1000],ch;
   int i,count=0;
   cout << "Enter a string: ";
   cin.getline(c, 1000);
   cout << "Enter a character to find frequency: ";
   cin >> ch;
   for(i=0;c[i]!='\0';++i)
   {
       if(ch==c[i])
           ++count;
   }
   cout << "Frequency of " << ch <<  " = " << count;
   return 0;
}

Output

Enter a string: i like programming
Enter a character to find frequency: i
Frequency of e = 3

❰ Previous Next ❱


C++

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website