C++ program to find the frequency of characters in a string

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

The user must enter both characters and strings at run-time.

Note: The frequency of a character in a string is the total number of times the character occurs in the given string. or the occurrence of a character in a string.

Find the Frequency of a Character in a String

To find the frequency of a character in a string using C++ programming, you have to ask the user to enter the string first, and then ask them 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 the 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 built and runs under the 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 the above program with the same user input (as provided in the sample run given above) goes like this:

Determine the Frequency of All Characters in a String

This program asks the user to enter the string alone. Because this program finds and prints the frequency of all characters in a 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 the preceding program, whenever a character matches a character at any index, we shift all the characters from that index back to one index.

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

Count each character and print the remaining string every time

This program is identical to the previous one. The only difference is that this program also prints the remaining string in the 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 the same user input as provided in the previous program:

count each character and print remaining string c++

The same program in different languages

C++ Quiz


« Previous Program Next Program »


Liked this post? Share it!