c program to print frequency of character in string

In this example, we take string as input and return frequency of character.

Find the Frequency of Characters in a String In C Programming

	
#include <stdio.h>
#include <string.h>
int main()
{
    char string[100];
    int c = 0, count[26] = {0}, x;

    printf("Enter a string ::");
    gets(string);

    while (string[c] != '\0') {

        if (string[c] >= 'a' && string[c] <= 'z') {
            x = string[c] - 'a';
            count[x]++;
        }
        c++;
    }

    for (c = 0; c < 26; c++)
            printf("%c occurs %d times in the string.\n", c + 'a', count[c]);
    return 0;
}
	

Output :

	
Enter a string ::a quick brown fox jumps over the lazy dog
a occurs 2 times in the string.
b occurs 1 times in the string.
c occurs 1 times in the string.
d occurs 1 times in the string.
e occurs 2 times in the string.
f occurs 1 times in the string.
g occurs 1 times in the string.
h occurs 1 times in the string.
i occurs 1 times in the string.
j occurs 1 times in the string.
k occurs 1 times in the string.
l occurs 1 times in the string.
m occurs 1 times in the string.
n occurs 1 times in the string.
o occurs 4 times in the string.
p occurs 1 times in the string.
q occurs 1 times in the string.
r occurs 2 times in the string.
s occurs 1 times in the string.
t occurs 1 times in the string.
u occurs 2 times in the string.
v occurs 1 times in the string.
w occurs 1 times in the string.
x occurs 1 times in the string.
y occurs 1 times in the string.
z occurs 1 times in the string.
	

Share your thoughts

Ask anything about this examples