Algorithm


1. Start
2. Declare a string variable to store the input string.
3. Declare an array of integers to store the frequency of each character (size 256 for ASCII characters).
4. Initialize the frequency array to zero.
5. Read the input string.
6. Loop through each character in the string.
   a. Increment the frequency of the current character in the frequency array.
7. Display the frequency of each character.
8. End

Code Examples

#1 Code Example- Find Frequency of Characters of a String Object

Code - C++ Programming

#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;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Number of a = 2

#2 Code Example- Find Frequency of Characters in a C-style String

Code - C++ Programming

#include <iostream>

using namespace std;
int main()
{
   char c[] = "C++ programming is not easy.", check = 'm';
   int count = 0;

   for(int i = 0; c[i] != '\0'; ++i)
   {
       if(check == c[i])
           ++count;
   }
   cout << "Frequency of " << check <<  " = " << count;
   return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Number of m = 2
Advertisements

Demonstration


C++ Programing Example to Find the Frequency of Characters in a String-DevsEnv