C Program to search occurrence of character in String
Program
#include<stdio.h>
void main()
{
char str[20], ch;
int count = 0, i;
printf("Enter a string (Without spaces):\t");
scanf("%s", &str);
printf("Enter the character to be searched:\t");
scanf(" %c", &ch);
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == ch)
count++;
}
if (count == 0)
printf("Character '%c' is not present", ch);
else
{
printf("Occurence of character '%c' is %d\n", ch, count);
}
}
Output
Enter a string: australia
Enter the character to be searched: a
Enter a string (Without spaces): oodlescoop
Enter the character to be searched: o
Occurence of character 'o' is 4