C Programming: Find maximum occurring character in a string
C String: Exercise-10 with Solution
Write a program in C to find the maximum number of characters in a string.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define str_size 100 // Declare the maximum size of the string
#define chr_no 255 // Maximum number of characters to be allowed
int main() {
char str[str_size]; // Declare a character array to store the string
int ch_fre[chr_no]; // Declare an array to store the frequency of characters
int i = 0, max; // Declare variables for iteration and finding the maximum frequency
int ascii; // Variable to hold the ASCII value of a character
printf("\n\nFind maximum occurring character in a string :\n"); // Display information about the task
printf("--------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin); // Read a string from the standard input (keyboard)
for (i = 0; i < chr_no; i++) { // Initialize the frequency of all characters to zero
ch_fre[i] = 0;
}
/* Read for frequency of each character */
i = 0;
while (str[i] != '\0') { // Loop until the end of the string ('\0' character is encountered)
ascii = (int)str[i]; // Get the ASCII value of the current character
ch_fre[ascii] += 1; // Increment the frequency count for that character
i++;
}
max = 0; // Initialize the index of the character with maximum frequency
for (i = 0; i < chr_no; i++) {
if (i != 32) { // Exclude space (' ') character
if (ch_fre[i] > ch_fre[max]) {
max = i; // Update the index of the character with maximum frequency
}
}
}
printf("The Highest frequency of character '%c' appears number of times : %d \n\n", max, ch_fre[max]); // Display the character with maximum frequency and its count
return 0; // Return 0 to indicate successful execution of the program
}
Sample Output:
Find maximum occurring character in a string : -------------------------------------------------- Input the string : Welcome to w3resource The Highest frequency of character 'e' appears number of times : 4
Flowchart:
C Programming Code Editor:
Improve this sample solution and post your code through Disqus.
Previous: Write a program in C to count total number of vowel or consonant in a string.
Next: Write a C program to sort a string array in ascending order.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/c-programming-exercises/string/c-string-exercise-10.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics