Category : C Source Code
Archive   : CSRC2.ZIP
Filename : INSTR.C

Output of file : INSTR.C contained in archive : CSRC2.ZIP
* i n s t r . c



title instr Get Index of a String In a String
index Get index of a string in a string


char s[]; /* Subject string */
char p[]; /* Pattern string */


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().



Jerry Leichter


* 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();

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)

  3 Responses to “Category : C Source Code
Archive   : CSRC2.ZIP
Filename : INSTR.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: