Finding occurrences of one string in another: strstr
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main() {
char text[100];
char substring[40];
int i = 0;
printf("\n string to be searched(less than 100 characters):\n");
gets(text);
printf("\n string sought (less than 40 characters ):\n");
gets(substring);
/* Convert both strings to upper case. */
for(i = 0 ; (text[i] = toupper(text[i])) != '\0' ; i++);
for(i = 0 ; (substring[i] = toupper(substring[i])) != '\0' ; i++);
printf("\nThe second string %s found in the first.\n",
((strstr(text, substring) == NULL) ? "was not" : "was"));
}
Related examples in the same category