C++ program to calculate frequency of characters in a string

Calculating frequency of characters in a string.

In this article we will learn how to write a C++ program to calculate the frequency of characters in a string. First we have to find out unique characters from the string as calculating frequency means how many times each character is present in the string. We will use a for loop that will count how many times every unique character is present in the string. Then to print the result we will use another for loop.

C++ program to calculate frequency of characters in a string

Algorithm:

  • Initialize the variables.
  • Accept the input.
  • Initialize a for loop.
  • This for loop will be used to count the number of time each character is present.
  • Terminate first at the end of string. 
  • Initialize another for loop to print the frequency if it is at least 1.

C++ programming code to calculate the frequency of characters in a string

 

#include <iostream>
using namespace std;

int main()
{
    //Initializing variables.
    char str[100];
    int i;
    int freq[256] = {0};
    
    //Accepting inputs.
    cout<<"Enter the string: ";
    gets(str);
    
    //Calculating frequency of each character.
    for(i = 0; str[i] != '\0'; i++)
    {
        freq[str[i]]++;
    }
    
    //Printing frequency of each character.
    for(i = 0; i < 256; i++)
    {
        if(freq[i] != 0)
        {
           cout<<"The frequency of "<<char(i)<<" is "<<freq[i]<<endl;
        }
    }
    return 0;
}
Output:
Enter the string: prepinsta
The frequency of a is 1
The frequency of e is 1
The frequency of i is 1
The frequency of n is 1
The frequency of p is 2
The frequency of r is 1
The frequency of s is 1
The frequency of t is 1

Operations on Strings

  • Length of the string without using strlen() function : C | C++ | Java | Python
  • Toggle each character in a string : C | C++ | Java | Python
  • Count the number of vowels : C | C++ | Java | Python
  • Remove the vowels from a String : C | C++ | Java | Python
  • Check if the given string is Palindrome or not : C | C++ | Java | Python
  • Print the given string in reverse order : C | C++ | Java | Python
  • Remove all characters from string except alphabets : C | C++ | Java | Python
  • Remove spaces from a string : C | C++ | Java | Python
  • Remove brackets from an algebraic expression : C | C++ | Java | Python
  • Count the sum of numbers in a string : C | C++ | Java | Python
  • Capitalize the first and last character of each word of a string : C | C++ | Java | Python
  • Calculate frequency of characters in a string : C | C++ | Java | Python
  • Find non-repeating characters in a string : C | C++ | JAVA | Python
  • Check if two strings are Anagram or not : C | C++ | Java | Python
  • Replace a sub-string in a string: C | C++ | Java | Python
  • Count common sub-sequence in two strings : C | C++ | Java | Python
  • Check if two strings match where one string contains wildcard characters : C | C++ | Java | Python
Operations on Strings