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 : "); scanf("%[^\n]", 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 Maximum Occurring Character in a String %s is '%c' %d times", str, result, max); return 0; }
Output

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 : "); scanf("%[^\n]", str); 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

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.
Share on
Also Prepare Below Important Question
- Java Program to Perform Left Rotation on Array Elements by Two
- Java Program to Perform Right Rotation on Array Elements by Two
- Java Program to Print Odd Numbers from Array
- Java Program to Print All Even Numbers in Array
- Java Program to Find the Sum of Array Elements
- Java Program to Delete Element of Array At Given Location
- Java Program to Delete a given Element of Array
- Java Program to Delete Element at End of Array
- Java Program to Insert Element in Array at given Location
- Java Program to Insert Element At the End of Array
- Java Program to Print Length of an Array
- Reverse Array without using Second Array or inplace Reversal Java Program
- Java Program to Print Array in Reverse Order
- Java Program to Sort String Character in Descending order
- Java Program to Sort String in Ascending Order
- Java Program to Print non Repeating Characters in String
- Java Program to Find Sum of Integers in the String
- Java Program to Remove Duplicates From String
- Java Program to Concatenate two Strings
- Java Program to Check if two Strings are Same
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