C Program to Find the Frequency of Characters in a String

#include <stdio.h>
#include <string.h>
int main()
{
	char str[100];
	int i;
	int freq[256] = {0};
	gets(str);
	for(i = 0; str[i] != '\0'; i++)
	{
		freq[str[i]]++;
	}
	for(i = 0; i < 256; i++)
	{
		if(freq[i] != 0)
		{
			printf("%c %d\n", i, freq[i]);
		}
	}
	return 0;
}

Output:

Enter the string : face
f - 1
a - 1
c - 1
e - 1

Leave a Comment