C program to search all occurrences of a character in a given string using a while loop.
C program to search all occurrences of a character in a given string using a while loop.
#include <stdio.h>
#define MAX 100
int main()
{
char text[MAX];
char search;
int i;
printf("\n Enter any string: ");
gets(text);
printf("\n Enter any character to search in the given string: ");
search = getchar();
i=0;
while(text[i]!='\0')
{
if(text[i] == search)
{
printf("'%c' is found at index %d\n", search, i);
}
i++;
}
return 0;
}
Output:
Enter any string: I love c programcoding
Enter any character to search: o
'o' is found at index 3
'o' is found at index 11
'o' is found at index 17