Find frequency of all characters in a string
C program to find the frequency of all characters in a string.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char str[MAX];
int i, freq[256] = {0};
printf("Enter the string\t:");
gets(str);
i = 0;
while(str[i] != '\0')
{
freq[str[i]]++;
i++;
}
for(i=0;i<256;i++)
{
if(freq[i] != 0)
{
printf("\nCharacter '%c' occurs %d times",i,freq[i]);
}
}
getch();
}
Output
Enter the string :welcome to coursecrux.com
Character ' ' occurs 2 times
Character '.' occurs 1 times
Character 'c' occurs 4 times
Character 'e' occurs 3 times
Character 'l' occurs 1 times
Character 'm' occurs 2 times
Character 'o' occurs 4 times
Character 'r' occurs 2 times
Character 's' occurs 1 times
Character 't' occurs 1 times
Character 'u' occurs 2 times
Character 'w' occurs 1 times
Character 'x' occurs 1 times