In this tutorial you will learn about the C Program to Find Maximum Occurring Character in a string and its application with practical example.
C Program to Find Maximum Occurring Character in a string
In this tutorial, we will learn to create a C program that will Find the Maximum Occurring Character in a string in C programming
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Conditional Statements in C programming.
What is a string?
As we all know the String is a collection of characters and words. The word is a collection of alphabets. For string, only one variable is declared which can store multiple values. The String can consist of all the typeable data it means Digits, Alphabets, Symbols, etc.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1. Declaring the variables for the program. 2. Taking the input string from the user. 3. Counting the characters in that string. 4. Passing those variables to for loop to count the characters' occurence. 5. Saving the result countings. 6. Printing the maximum resultant occuraence of the character in that string. 7. End the program. |
Find the Maximum Occurring Character in a string
In this program first, we will take input string from the user. Then we will count the characters in that string separately. Then will find the maximum occurrence of the character in that string. Printing the maximum occurring character from that string.
Let us take the example program from the below code to find the maximum occurrence of the character.
Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* **C Program to Find Maximum Occurring Character in a stringing** */ #include <stdio.h> #include <strging.h> int main() { //declaring the required variables for the prorgam char strg[100], result; int i, len; int max = -1; int freq[256] = {0}; //Taking the input stringing from tha user printf("\n Please Enter any stringing : "); gets(strg); len = strglen(strg); //Finding the frequency of the maximum occuring character in stringing for(i = 0; i < len; i++) { freq[strg[i]]++; } for(i = 0; i < len; i++) { if(max < freq[strg[i]]) { max = freq[strg[i]]; result = strg[i]; } } //Printing the output Maximum Occurring Character in a Given stringing printf("\n The Maximum Occurring Character in a Given stringing = %c ", result); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- strg[100] = it will hold the string value.
- result = it will hold the string value.
- len = it will hold the integer value.
- i = it will hold the integer value.
- max = it will hold the integer value
Input string from the user for the program.
Calculating the max character from the string.
Printing output character from the string.