C program to count the total number of occurrences of a character in a string using a while loop.
C program to count the total number of occurrences of a character in a string using a while loop.
#include <stdio.h>
#define MAX 100
int main()
{
char text[MAX];
char search;
int i, count;
printf("\n Enter any string: ");
gets(text);
printf("\n Enter any character to search: ");
search = getchar();
count = 0;
i=0;
while(text[i] != '\0')
{
if(text[i] == search)
{
count++;
}
i++;
}
printf("\n Total occurrence of words in the given string '%c' = %d", search, count);
return 0;
}
Output:
Enter any string: I love c programming language
Enter any character to search: o
Total occurrence of words in the given string 'o' = 2