C++ Program to Find the Frequency of Characters in a String

#include <iostream>
using namespace std;
int main()
{
    string str = "C++ Programming is awesome";
    char checkCharacter = 'a';
    int count = 0;
    for (int i = 0; i < str.size(); i++)
    {
        if (str[i] ==  checkCharacter)
        {
            ++ count;
        }
    }
    cout << "Number of " << checkCharacter << " = " << count;
    return 0;
}

OUTPUT:

Number of a = 2

Sharing Is Caring

Leave a Comment