Easy Tutorial
❮ C Function Ftell C Function Sqrt ❯

C Language Example - Finding the Number of Occurrences of a Character in a String

C Language Examples

Find the starting position of a character in a string (index starts from 0).

Example

#include <stdio.h>

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

   printf("Enter a string: ");
   fgets(str, (sizeof str / sizeof str[0]), stdin);

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

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

   printf("The character %c appears %d times in the string", ch, frequency);

   return 0;
}

Output:

Enter a string: tutorialpro
Enter the character to find: o
The character o appears 2 times in the string

C Language Examples

❮ C Function Ftell C Function Sqrt ❯