C++ Program to Find Frequency of Characters in String

In this article, you will learn and get code to find and print frequency of character (single and all) in string using C++ language. Here are the list of programs available in this article:

  • Find Frequency of a character in String
  • Find Frequency of all characters in String
  • Find Frequency of each character and print remaining string side by side

Note - Frequency of a character in string means total number of times, the character occurred in given string. Or the occurrence of a character in string.

Note - Both character and string must be entered by user at run-time

Find Frequency of a Character in String

To find the frequency of a character in string using C++ programming, you have to ask from user to enter the string first and then ask to enter the character to find its frequency as shown in the program given below:

The question is, write a program in C++ to find frequency of a character in a string. Here is its answer:

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char str[100], ch;
    int i=0, freq=0;
    cout<<"Enter the String: ";
    gets(str);
    cout<<"Enter a Character to Find its Frequency: ";
    cin>>ch;
    while(str[i])
    {
        if(ch==str[i])
            freq++;
        i++;
    }
    cout<<"\nFrequency = "<<freq;
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program find frequency of character

Now supply any string say codes cracker dot com and then a character say c to find its frequency after pressing the ENTER key. Here is the sample output:

find frequency of character in string c++

The dry run of above program with same user input (as provided in sample run given above) goes like:

  • Initial values, i=0 and freq=0
  • When user enters the string, then it gets stored to str[] in this way:
    • str[0]=c
    • str[1]=o
    • str[2]=d
    • str[3]=e
    • str[4]=s
    • str[5]=' '
      A space
    • str[6]=c
    • str[7]=r
    • and so on upto
    • str[20]=m
  • And when user enters a character say c, then it gets stored in ch
  • Now the execution of while loop gets started. Its execution continues until its condition (str[i]) evaluates to be false. When the value of i becomes 21, then because at 21th index, there is no any character left, then its condition evaluates to be false
  • At first run of while loop, the condition str[i] or str[0] (on putting the value of i) or c evaluates to be true, therefore program flow goes inside the loop
  • And the condition of if, that is ch==str[i] or c==str[0] or c==c evaluates to be true, therefore program flow goes inside the if's body and the value of freq gets incremented. So freq=1
  • Now the value of i also gets incremented and program flow goes back and evaluates the condition of while loop again with new value of i (1)
  • That is, the condition str[i] or str[1] or o evaluates to be true again, therefore again program flow goes inside the loop
  • But this time, the condition of if, that is ch==str[i] or c==str[1] or c==o evaluates to be false, therefore the value of freq does not gets incremented. So only the value of i gets incremented. Now i=2 and program flow goes back and evaluates the condition while loop again
  • This process continues, until its condition evaluates to be false
  • Every-time while entering into its body, we've compared the value of ch with each and every character available in the string str. If character gets matched, then the value of freq gets incremented
  • Therefore after exiting from the loop, we'll have a variable freq that holds the frequency of character. So just print its value as output.

Find Frequency of All Characters in String

This program asks from user to enter the string only. Because this program find and prints frequency of all characters of given string:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    char str[100], ch;
    int i, j, k, len, freq=0;
    cout<<"Enter the String: ";
    gets(str);
    len = strlen(str);
    cout<<"\nCharacter\tFrequency";
    for(i=0; i<len; i++)
    {
        ch = str[i];
        for(j=0; j<len; j++)
        {
            if(ch==str[j])
            {
                freq++;
                for(k=j; k<(len-1); k++)
                    str[k] = str[k+1];
                len--;
                str[len] = '\0';
                j--;
            }
        }
        cout<<endl<<ch<<"\t\t"<<freq;
        freq=0;
        i--;
    }
    cout<<endl;
	return 0;
}

Here is its sample run with user input, codes cracker dot com:

find frequency of each character in string c++

In above program, every time when a character getting matched with a character at any index, we've shifted all the character from its next index to one index back.

Note - Don't forgot to initialize a null terminated character (\0) at last index of the new string.

Count Each Character and Print Remaining String Every time

This program is same as of previous program. The only difference is, this program also prints the remaining string in third column:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    char str[100], ch;
    int i, j, k, len, freq=0;
    cout<<"Enter the String: ";
    gets(str);
    len = strlen(str);
    cout<<"\nCharacter\tFrequency\tRemaining String";
    for(i=0; i<len; i++)
    {
        ch = str[i];
        for(j=0; j<len; j++)
        {
            if(ch==str[j])
            {
                freq++;
                for(k=j; k<(len-1); k++)
                    str[k] = str[k+1];
                len--;
                str[len] = '\0';
                j--;
            }
        }
        cout<<endl<<ch<<"\t\t"<<freq<<"\t\t"<<str;
        freq=0;
        i--;
    }
    cout<<endl;
	return 0;
}

Here is its sample run with same user input as provided in previous program:

count each character and print remaining string c++

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »