Find the lowest frequency character in a string with C.
#include <stdio.h>
int main()
{
char str[100];
int freq[200]; // Stores frequency of each character
int i = 0, min;
int ascii;
printf("Enter any string: ");
gets(str);
/* Initialize 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 minimum frequency */
min = 0;
for (i = 0; i < 200; i++)
{
if (freq[i] != 0)
{
if (freq[min] == 0 || freq[i] < freq[min])
min = i;
}
}
printf("Minimum occurring character is '%c' = %d.", min, freq[min]);
return 0;
}
Output
Enter any string : learn with reza
Maximum occurring character is ' ' = 2 times.
0 Comments