C++ Program to find frequency of character in string


In this program, You will learn how to find frequency of character in string in C++.


Enter string: Hello

Enter a character: l

Total Frequency is: 2


Example: How to find frequency of character in string in C++

Run it Now
#include<iostream>
#include<cstring>
using namespace std;

int main() {

    char a[100], b;
    int j, i = 0, c = 0;

    cout << "Enter a String :";
    cin.getline(a, 100);

    cout << "\nEnter a Character :";
    cin>>b;

    i = strlen(a);
    for (j = 0; j < i; j++) {
        if (a[j] == b)
            c++;
    }
    cout << "\nTotal Frequency is :" << c;

    return 0;
}

Output:

Enter a String :level
Enter a Character :e
Total Frequency is :2