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]
Also Prepare Below Important Question
- Sum of digits of Given Number in Java
- Hibernate Interview Questions for 2+ years of experience
- 68 Most Important Microservices Interview Questions
- 60 Most Important Git Interview Questions
- 50+ Mostly asked Java Interview Questions for 6 Years Exp.
- 60+ Mostly Asked Java Interview Questions for 4 Years Exp
- 60+ Mostly asked Java Interview Question for 3+ Years
- 60+ Spring Boot interview questions for 4+ years Exp.
- 60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
- Scenario Based Java 8 Coding Interview Questions (For Experienced)
- Python Program to add two numbers without addition operator
- Mostly Asked Java Interview Questions For 2 Yrs Experience
- Find All Pairs in Array whose Sum is Equal to given number Using Java
- Java Program to find GCD of two Numbers using Recursion
- Python Program to Separate Characters in a Given String
- 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
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