C program to find the highest frequency character in a string – In this article, we will detail in on the multiple means to find the highest frequency character in a string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The means used in this distinct piece are as follows:
- Using Standard Method
- Using Function
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As you can see in the image uploaded above, firstly, you need to enter the string successfully.
The string entered here is as follows:
“hello welcome to cbeginners”
As you can see, there are quite a few characters which are repeated but the character ‘e’ is repeated the most.
It is used for 5 times.
Thus, the maximum frequency is of ‘e’: 5 times.
Hence, doing the same in C programming is as follows:
Using Standard Method
- Initialize count=0,k=0.Read the entered string and store the string into the variable s[1000] using gets(s).
2) The for loop iterates the string until the last character of the string becomes to null. Then initialize the j value to n, which is the length of the string.
3) a) Iterate the outer for loop through the string with the structure for(i=0;i<n;i++).
b) Inner for loop compares the remaining characters of the string with s[i]. If it matches then increase the count value by 1.
c) Place each character frequency in the integer array a[1000].
d) Initialize k with maximum frequency.
e) If any frequency value of a[1000] match with the k then print that element and k value.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include <stdio.h> #include <string.h> int main() { char s[1000]; int a[1000],i,j,k=0,count=0,n; printf("Enter the string : "); gets(s); for(j=0;s[j];j++); n=j; for(i=0;i<n;i++) { a[i]=0; count=1; if(s[i]) { for(j=i+1;j<n;j++) { if(s[i]==s[j]) { count++; s[j]='\0'; } } } a[i]=count; if(count>=k) k=count; } printf("maximum occuring characters "); for(j=0;j<n;j++) { if(a[j]==k) { printf(" '%c',",s[j]); } } printf("\b=%d times \n ",k); return 0; } |
Output:
1 2 |
Enter the string: hello welcome to college maximum occurring characters 'e'=5 times |
Using Function
- The function stringlength(char *s) returns the length of the string.
2) The main() function calls the printmax(char *s) by passing the string as an argument to the function.
3) The function will print the highest frequency elements.
a) The outer for loop iterates through the string, the inner loop finds the frequency of each character and store the frequencies in the string a[1000].
b) initialize the k with maximum frequency.
4) If the frequency of any character is equal to the k value then print that character and k value.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include <stdio.h> #include <string.h> int stringlength(char *s) { int j; for(j=0;s[j];j++); return j; } void printmax(char *s) { int a[1000],i,j,k=0,count=0,n; n=stringlength(s); for(i=0;i<n;i++) { a[i]=0; count=1; if(s[i]) { for(j=i+1;j<n;j++) { if(s[i]==s[j]) { count++; s[j]='\0'; } } } a[i]=count; if(count>=k) k=count; } printf("maximum occuring characters "); for(j=0;j<n;j++) { if(a[j]==k) { printf(" '%c',",s[j]); } } printf("\b=%d times \n ",k); } int main() { char s[1000]; printf("Enter the string : "); gets(s); printmax(s); return 0; } |
1 2 |
Enter the string : abcd abcd abcd abcd . maximum occuring characters 'a', 'b', 'c', 'd', ' '=4 times |