C Program to Find the Frequency of Characters in a String

@codevarsity
 · 
2.1k views
 · 

In this sample C program asks user to enter a string and a character and checks how many times the character is repeated in the string.

Code

#include <stdio.h>

int main()
{
   char str[1000], ch;
   int i, frequency = 0;

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

   printf("Enter a character to find the frequency: ");
   scanf("%c",&ch);

   for(i = 0; str[i] != '\0'; ++i)
   {
       if(ch == str[i])
           ++frequency;
   }

   printf("Frequency of %c = %d", ch, frequency);

   return 0;
}

Output

Enter a string: This website is awesome.
Enter a character to find the frequency: e
Frequency of e = 4

Comments