Program to find frequency of characters in a string C++

Finding frequency of characters in a string C++ is like:

Let the string is: I am Software engineer

The Frequency if character ‘e’ in this String is: 4

Program to find frequency of characters in a string C++

CODE:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char c[30],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]!='';++i)
    {
        if(ch==c[i])
        count++;
    }
    cout << "Frequency of " << ch << " = " << count;
    return 0;
}

 

OUTPUT:

frequency of characters in a string c++

Leave a Reply