1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <stdio.h>
#include <string.h>
char tracks[][80] = {
"I left my heart in Harvard Med School",
"Newark, Newark - a wonderful town",
"Dancing with a Dork",
"From here to maternity",
"The girl from Iwo Jima",
};
void find_track(char search_for[])
{
printf("search_for: %s\n\n", search_for);
int i;
for (i = 0; i < 5; i++) {
printf("%p\t", (strstr(tracks[i], search_for)));
printf("tracks[%i]: %s\n", i, tracks[i]);
if ( strstr(tracks[i], search_for) )
printf("\n\t\t ---> Track %i: '%s' <--- \n\n", i, tracks[i]);
}
}
int main()
{
char search_for_m[80];
printf("Search for: ");
fgets(search_for_m, 80, stdin);
/*
* It reads the '\n' at the end, when you hit ENTER, if input_length <=79.
*/
char *nlp = strchr(search_for_m,'\n');
if (nlp != NULL)
*nlp = '\0';
find_track(search_for_m);
return 0;
}
|
Search for: town
search_for: town
00000000 tracks[0]: I left my heart in Harvard Med School
0040206D tracks[1]: Newark, Newark - a wonderful town
---> Track 1: 'Newark, Newark - a wonderful town' <---
00000000 tracks[2]: Dancing with a Dork
00000000 tracks[3]: From here to maternity
00000000 tracks[4]: The girl from Iwo Jima
Process returned 0 (0x0) execution time : 3.612 s
Press any key to continue.
|