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.
/*
* C Program To Find the Highest Frequency Character in a String
*/
#include <stdio.h>
#include <string.h>
char string1[100], visited[100];
int count[100] = {0}, flag = 0;
void main()
{
int i, j = 0, k = 0, l, max, index;
printf("Enter a string : ");
scanf("%[^\n]s", string1);
l = strlen(string1);
for (i = 0; i < l; i++)
{
if (i == 0)
{
visited[j++] = string1[i];
count[j - 1]++;
}
else
{
for (k = 0; k < j; k++)
{
if (string1[i] == visited[k])
{
count[k]++;
flag = 1;
}
}
if (flag == 0)
{
visited[j++] = string1[i];
count[j - 1]++;
}
flag = 0;
}
}
for (i = 0; i < j; i++)
{
if ((i == 0) && (visited[i] != ' '))
{
max = count[i];
continue;
}
if ((max < count[i]) && (visited[i] != ' '))
{
max = count[i];
index = i;
}
}
printf("\nMax repeated character in the string = %c ", visited[index]);
printf("\nIt occurs %d times", count[index]);
}
$ 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.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy C Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Buy Computer Science Books