#include #include int findpos(sentence, word, startpos) char *sentence; char *word; int startpos; { char *p; /* find the space after the first word */ p = strchr(sentence, ' '); /* No more spaces means we ran out of words */ if (p == NULL) return(0); /* Find position of next word */ p++; /* If first word in the sentence is the one we want, return * its position.*/ if (strncmp(sentence, word, strlen(word)) == 0) return(startpos); else /* else, call ourselves again, this time with the first word * chopped off from the sentence */ return(findpos(p, word, startpos + 1)); } char *s = "foo bar baz bletch"; char *w = "baz"; main() { printf("position: %d\n", findpos(s, w, 1)); exit(0); }