C Program to Find Maximum Occurring Character in a String

Write a C Program to Find Maximum Occurring Character in a String with example.

C Program to Find Maximum Occurring Character in a String Example 1

This program allows the user to enter a string (or character array). Next, it will find the maximum occurring character (most repeated character) inside a string.

#include <stdio.h>
#include <string.h>

int main()
{
char str[100], result;
int i, len;
int max = -1;

int freq[256] = {0};

printf("\n Please Enter any String : ");
gets(str);

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 The Maximum Occurring Character in a Given String = %c ", result);

return 0;
}
C Program to Find Maximum Occurring Character in a String 1

First, we declared a Freq array of size 256, which will initially hold 0’s. Next, we used For Loop to iterate every character in a String, and find the maximum repeated character.

for(i = 0; i < len; i++)
{
	if(max < freq[str[i]])
	{
		max = freq[str[i]];
		result = str[i];
	}
}

str[] = hello

First For Loop in C – First Iteration: for(i = 0; i < strlen(str) ; i++)
The condition is True because 0 < 5.
if(max < freq[str[i]]) => if(-1 < freq[h]])
=> if(-1 < 1) – Condition is True
max = freq[str[i]] => 1
result = str[i] = h

Second Iteration: for(i = 1; 1 < 5; 1++)
if(max < freq[str[1]])  => if(1 < freq[e]])
=> if(1 < 1) – Condition is False

Third Iteration: (i = 2; 2 < 5; 2++)
if(1 < freq[l]]) => if(1 < 2) – Condition is True
max = freq[str[i]] => 2
result = str[i] = l

Fourth Iteration: for(i = 3; 3 < 5; 3++)
if(max < freq[str[3]])  => if(2 < freq[l]])
=> if(2 < 2) – Condition is False

Fifth Iteration: for(i = 4; 4 < 5; 4++)
if(max < freq[str[4]])  => if(2 < freq[o]])
=> if(2 < 1) – Condition is False

At last we used the C Programming printf statement to print the final string in C.

printf("\n The Maximum Occurring Character in a Given String = %c ", result);

C Program to Find Maximum Occurring String Character Example 2

This Maximum Occurring String Character C program is same as above, but we did small changes to the logic.

/* Find Maximum Occurring Character in a String */

#include <stdio.h>
#include <string.h>

int main()
{
char str[100], result;
int i, len;
int max = 0;

int freq[256] = {0};

printf("\n Please Enter any String : ");
gets(str);

len = strlen(str);

for(i = 0; i < len; i++)
{
freq[str[i]]++;
}

for(i = 0; i < 256; i++)
{
if(freq[i] > freq[max])
{
max = i;
}
}
printf("\n Character '%c' appears Maximum of %d Times in a Given String : %s ", max, freq[max], str);

return 0;
}
 Please Enter any String :  tutorialgateway.org

 Character 'a' appears Maximum of 3 Times in a Given String :  tutorialgateway.org 

Program to Find Maximum Occurring Character in a String Example 3

This program is the same as the second example, but this time we used the Functions concept to separate the logic.

 
#include <stdio.h>
#include <string.h>

void Max_Occurring(char *str);

int main()
{
char str[100];

printf("\n Please Enter any String : ");
gets(str);

Max_Occurring(str);

return 0;
}

void Max_Occurring(char *str)
{
int i;
int max = 0;

int freq[256] = {0};

for(i = 0; str[i] != '\0'; i++)
{
freq[str[i]] = freq[str[i]] + 1;
}

for(i = 0; i < 256; i++)
{
if(freq[i] > freq[max])
{
max = i;
}
}
printf("\n Character '%c' appears Maximum of %d Times in a Given String : %s ", max, freq[max], str);
}
 Please Enter any String :  learn c programming at tutorial gateway

 Character 'a' appears Maximum of 6 Times in a Given String :  learn c programming at tutorial gateway 

Also Read