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.
Also Prepare Below Important Question
- Python Program to add two number using Recursion
- Python Program to Find Highest Frequency Element in Array
- Python Program to Merge two Arrays
- Perform left rotation by two positions in Array Using Python
- Python Program to Delete Element at Given Index in Array
- Python Program to Delete element at End of Array
- Python Program to Copy one String to Another String
- Python Program to Remove Repeated Character from String
- Print highest frequency Character in String in Python
- Python Program to Convert lowercase vowel to uppercase
- Convert Celsius to Fahrenheit in Python
- Leap Year Program in Python
- Python Program to convert Decimal to Octal number
- Python Program to Convert Decimal Number into Binary
- Find all Pairs Whose Sum is Equal to Given number in Python
- Python Program to Replace First Vowel With ‘-‘ in String
- Python Program to find Prime factors of given integer
- Python Program to Print Prime Number in given range
- Java Program to Perform Left Rotation on Array Elements by Two
- Java Program to Perform Right Rotation on Array Elements by Two
Interview Questions Categories
C Programming Interview Preparation
Core Java Programming Interview Preparation
- Core Java Programming Coding Questions
- Core Java Pattern Programming Questions
- Core Java Programming Interview Questions