Best C++ Program To Find Frequency of A Character in String

Frequency of a character means how many times a specific character occurred in a string.

For example, if the given string is “Hello World!” and we are asked to find the frequency of character ‘o’ then we can easily find that the specific character occurred 2 times in the given string. Similarly, if someone asks that how many times the character ‘W’ is occurring in the string. We can conclude that ‘W’ has been occurred only once in the string. So, the frequency of ‘W’ is 1.

We can do the same thing in C++ programming language easily.

Just think about the logic behind finding the occurrences of a character. Think, how you found the character frequency in the examples stated above.

OK, I tell you. You just read the string carefully and counted the number of occurrences of specific character.

Same thing can be implemented efficiently in C++ program.

Before writing source code, it is good to find the needs of program.

Our program should capable of these two things:

1: Read the String.

2: Count the number of occurrences of specific character.

Now you have logic and probably most of you people can write code on your own. But, for those who cannot write code, I have uploaded source code below for reference.

Don’t leave, I also explained every bit of program code further in this article.

C++ Code To Count Frequency of A Character in String:

#include <iostream>
using namespace std;
int main()
{
    string givenStr="Computer Science";
    char charToCheck='i';
    int count=0;
    cout<<"Given String = "<<givenStr;
    for (int i=0;i<givenStr.size();i++)
    {
        if (givenStr[i]==charToCheck)
        {
              ++count;
        }
    }
    cout<<"\n==============================="<<endl;
    cout<<charToCheck<<" Occured "<<count<<" Times.";
    return 0;
}

Program Output:

Output of C++ Program To Find Frequency of A Character in String

Source Code Explanation:

First of all, we declared a string and initialized it to “Computer Science”. We are going to check occurrences of our desired character in this specific string.

We also declared a char type variable which contains the character to be checked in the string.

After that, we declared one int type variable and it would work as a counter in the program. Initially its value is zero because we don’t have even a single occurrence yet.

Recommended reading: Tower of Hanoi Game Project in C++ With Full Source Code

Next, we started a loop which runs as many times as the number of characters of string found with the help of size() function. This loop accesses every single character and compares it to chartocheck.

If both characters are same then count is increased by one i.e. one more occurrence is detected.

This process is repeated till the end of string. At the end, frequency of specific character is printed on the screen.

One more thing to remember is that A computer distinguishes the characters written in small case and upper case. For example, Computer thinks that ‘a’ is a character and ‘A’ is some other character.

For example, when we check for ‘c’ in our given string “Computer Science”. It gives us answer 2. In fact, our string contained three occurrences of ‘c’, 2 times for small letter and one for upper case. But program returned us the occurrences of small case letter because that is what we opted for.

Find Occurrences Character in String in C++

Now, if we check for ‘C’, we would get 1.

Count Frequency of Character in String in C++
The logic behind this is the ASCII codes for keys. You can learn more about it here.

It is advised to make small changes in code and see what happens.

Now, If you think this article is even a bit useful it is requested to must share the information.

You can also Click Here To Download Exercise Files.