Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!asuvax!hrc!valley!pfluegerm From: pfluegerm@valley.UUCP (Mike Pflueger) Newsgroups: comp.sys.mac.programmer Subject: Re: Hashing.... Summary: useful mod Message-ID: <45ea3155.15840@valley.UUCP> Date: 28 Sep 89 21:31:14 GMT References: <4345@internal.Apple.COM> <1728@neoucom.UUCP> Distribution: na Organization: gte Lines: 42 In article <1728@neoucom.UUCP>, sam@neoucom.UUCP (Scott A. Mason) writes: > ----------cut here----------- > /* searchfor returns 1 if substring is a substring of string. It returns 0 > otherwise. The theory behind searchfor is to search for the first character > of the substring in the source string, and then compare from there. This > helps speed of the process reducing the calls to strncmp. */ > > searchfor (substring, string) > char substring[], string[]; > { > int sublen, len, position; > char *index, firstchar; > > len = strlen (string); > sublen = strlen (substring); > /* only process substrings with the right first character */ > firstchar = substring[0]; > position = 0; > while (position < len) { > if (string[position] != firstchar) { > position++; > continue; > } > index = &(string[position]); > if (! (strncmp (index, substring, sublen)) ) { > return (1); /* the string was found */ > } > position++; > } > return (0); /* the string was not found */ > } /* searchfor */ A simple variation on this is often highly useful - if the substr is found, return POSITION so the calling routine can get at the substr easily. You can still check for 0 (or NOT zero) to find out if the substr is contained. -- Mike Pflueger @ AG Communication Systems (formerly GTE Comm. Sys.), Phoenix, AZ UUCP: {...!ames!ncar!noao!asuvax | uunet!zardoz!hrc | att}!gtephx!pfluegerm Work: 602-582-7049 FAX: 602-581-4850 Packet: WD8KPZ @ W1FJI