Search all occurrences of a character in a given string with C.
#include <stdio.h>
int main()
{
char str[100];
char toSearch;
int i;
printf("Enter any string: ");
gets(str);
printf("Enter any character to search: ");
toSearch = getchar();
i = 0;
while (str[i] != '\0')
{
if (str[i] == toSearch)
{
printf("'%c' is found at index %d\n", toSearch, i);
}
i++;
}
return 0;
}
Output
Enter any string : lear with Reza
Enter any character to search : e
'e' is found at index 1
'e' is found at index 11
0 Comments