/* * Simple program demonstrating use of string function and pointer * arithmetic. */ #include #include /* * look for needle in haystack and print index if found */ void foobar(char * haystack, char * needle) { char * p = strstr(haystack, needle); if (p == NULL) { printf("in '%s' did not find '%s'\n", haystack, needle); } else { /* * p points to start of "needle" in "haystack" if found, * so the following line finds its index */ int index = p - haystack; printf("in '%s' found '%s' at %d\n", haystack, needle, index); } } int main(void) { foobar("well hello there world", "hello"); foobar("hello", "hello"); foobar("goodbye all", "hello"); return 0; }