843
In this article, you will learn to find the frequency of characters in the string in C.
To find the frequency of characters in a string in C, we create an auxiliary array that stores the frequency of every character. The array is initialized with 0, and the string is traversed once to update the array.
Find the Frequency of a Character
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);
for (int i = 0; str[i] != '\0'; ++i) {
if (ch == str[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}
Output
Enter a string: This website is very helpful. Enter a character to find its frequency: e Frequency of e = 4