/* * i n s t r . c */ /*)LIBRARY */ #ifdef DOCUMENTATION title instr Get Index of a String In a String index Get index of a string in a string synopsis int instr(s,p) char s[]; /* Subject string */ char p[]; /* Pattern string */ description instr(s,p) returns the index of the first occurence of *p in *s, or -1 if p is not a substring of s. (The index is the offset to the beginning of the occurence; for example, instr("abcd","bc")==1.) If strlen(p) == 0, instr() returns 0. See also after(). bugs author Jerry Leichter #endif /* * )EDITLEVEL=13 * Edit history * 0.0 5-May-81 JSL Invention * 0.1 7-May-81 JSL Fix the stupid bugs * 0.2 18-May-81 JSL Explicitly declare match() * 0.3 23-Jun-81 JSL Conversion to new documentation conventions * 1.0 14-Jul-82 JSL Use strchr() instead of the old index() * 1.1 19-Jul-82 JSL Minor speedup - code scrunched */ #define NULL 0 #define EOS '\0' extern char *match(); instr(s,p) register char s[]; /* Subject */ char p[]; /* Pattern */ { register char *ss; if (p[0] == EOS) /* Null pattern... */ return(0); /* matches at start */ ss = s; while ((ss = strchr(ss,p[0])) != NULL) if (match(++ss,&p[1]) != NULL) return(ss-1-s); return(-1); }