C Program to Find Highest Frequency Character in a String

«
»
This C Program finds highest frequency character in a string. Here this program checks which character has occured more number of times and checks how many times these character has occured.

Here is source code of the C Program to find the highest frequency character in a string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program To Find the Highest Frequency Character in a String
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. char string1[100], visited[100];
  8. int count[100] = {0}, flag = 0;
  9.  
  10. void main()
  11. {
  12.     int i, j = 0, k = 0, l, max, index;
  13.  
  14.     printf("Enter a string : ");
  15.     scanf("%[^\n]s", string1);
  16.  
  17.     l = strlen(string1);
  18.  
  19.     for (i = 0; i < l; i++)
  20.     {
  21.         if (i == 0)
  22.         {
  23.             visited[j++] = string1[i];
  24.             count[j - 1]++;
  25.         }
  26.         else
  27.         {
  28.             for (k = 0; k  < j; k++)
  29.             {
  30.                 if (string1[i] == visited[k])
  31.                 {
  32.                     count[k]++;
  33.                     flag = 1;
  34.                 }
  35.             }
  36.             if (flag == 0)
  37.             {
  38.                 visited[j++] = string1[i];
  39.                 count[j - 1]++;
  40.             }
  41.             flag = 0;
  42.         }
  43.     }    
  44.  
  45.     for (i = 0; i < j; i++)
  46.     {
  47.         if ((i == 0) && (visited[i] != ' '))
  48.         {
  49.             max = count[i];
  50.             continue;
  51.         }
  52.         if ((max < count[i]) && (visited[i] != ' '))
  53.         {
  54.             max = count[i];
  55.             index = i;
  56.         }
  57.     }
  58.  
  59.     printf("\nMax repeated character in the string = %c ", visited[index]);
  60.     printf("\nIt occurs %d times", count[index]);
  61. }

advertisement
$ cc string23.c
$ a.out
Enter a string : Welcome to Sanfoundry's C Programming Class !
 
Max repeated character in the string = o 
It occurs 4 times

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.