Find the highest frequency character in a string with C.
#include <stdio.h>
int main()
{
char str[100];
int freq[200]; // Store frequency of each character
int i = 0, max;
int ascii;
printf("Enter any string: ");
gets(str);
/* Initializes frequency of all characters to 0 */
for (i = 0; i < 200; i++)
{
freq[i] = 0;
}
/* Finds frequency of each characters */
i = 0;
while (str[i] != '\0')
{
ascii = (int)str[i];
freq[ascii] += 1;
i++;
}
/* Finds maximum frequency */
max = 0;
for (i = 0; i < 200; i++)
{
if (freq[i] > freq[max])
max = i;
}
printf("Maximum occurring character is '%c' = %d times.", max, freq[max]);
return 0;
}
Output
Enter any string : Learn With Rezaul Karim
Maximum occurring character is ' ' = 3 times.
0 Comments