codescracker
c++

C++ Program to Find Frequency of Characters in String



« Previous Program Next Program »


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:

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:

// C++ Program to Find Frequency of a Character
// ----codescracker.com----

#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:

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:

// C++ Find Frequency of Each Character in String
// ----codescracker.com----

#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, everytime 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 Everytime

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

// Count Each Character and Print Remaining String
// ----codescracker.com----

#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


« Previous Program Next Program »