C program to print the highest frequency character in a String

In this tutorial we will learn writing program in C  to print the highest frequency character In A String with count. This program will be little bit tricky. We will have one integer variable which will store the counter and one char variable to store character.

How our highest frequency char counter program will work?

  • Our program will take a string as an input to count the highest occurred character.
  • Logic will be like we have one extra array to store the counter of the each character.
  • The extra array index will be mapped with the character array.
  • Now we will compare the counter of each character and print the maximum one.

Program in C to print highest frequency character in String with count

#include <stdio.h>
#include <string.h>
 
int main()
{
    char str[100], result;
    int i, len;
    int max = 0;
    int freq[256] = {0};
    printf("C Program to Find Maximum Occurring Character in a String \n");
    printf("Please Enter a String :  ");
    fgets(str, 100, stdin);
    len = strlen(str);
    for(i = 0; i < len; i++)
    {
        freq[str[i]]++;
    }
    for(i = 0; i < len; i++)
    {
        if(max <= freq[str[i]])
        {
            max = freq[str[i]];
            result = str[i];
        }
    }
    printf("\n Maximum Occurring Character in a String %s is '%c'  %d times", str, result, max);
    return 0;
}

Output

C Program to Find Maximum Occurring Character in a String
Please Enter a String :  Educational Website

 Maximum Occurring Character in a String Educational Website
 is 'e'  2 times

Limitations: There is one limitations in the above program. Suppose in a given string there might be two character which has same occurrence so in that case our program should print both character. But in the above program it will print only one. So to resolve this issue we have done changes in the logic. So please check the below program

C Program to print maximum occurrence character in string with count

#include <stdio.h>
#include <string.h>
int main()
{
    char str[256];
    int  temp[256],i,j,k=0,count=0,n,len;
    printf("C Program to Find Maximum Occurring Character in a String \n");
    printf("Please Enter a String :  ");
    fgets(str, 100, stdin);
    len = strlen(str);
    for(i=0; i<len; i++)
    {
        temp[i]=0;
        count=1;
        if(str[i])
        {
            for(j=i+1; j<len; j++)
            {
                if(str[i]==str[j])
                {
                    count++;
                    str[j]='\0';
                }
            }
        }
        temp[i]=count;
        if(count>=k)
            k=count;
    }
    printf("Maximum Occurring Character in a String");
    for(j=0; j<len; j++)
    {

        if(temp[j]==k)
        {
            printf(" '%c',",str[j]);
        }
    }
    printf("\b= %d times \n ",k);
    return 0;
}

Output

C Program to Find Maximum Occurring Character in a String 
Please Enter a String :  Quescol Education
Maximum Occurring Character in a String 'u', 'c', 'o'= 2 times

In the above example you can see we have given “hello java” as an input string. Here l and a has occurred 2 times. So in output you can see both the character has printed which was not happening in the first program.


[wpusb]