Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cwjcc!neoucom!sam From: sam@neoucom.UUCP (Scott A. Mason) Newsgroups: comp.sys.mac.programmer Subject: Re: Hashing.... Message-ID: <1728@neoucom.UUCP> Date: 26 Sep 89 12:45:50 GMT References: <4345@internal.Apple.COM> Reply-To: sam@neoucom.UUCP (Scott A. Mason) Distribution: na Organization: Northeastern Ohio Universities College of Medicine Lines: 41 In article <4345@internal.Apple.COM> athos@apple.com (Rick Eames) writes: >I am a relatively new programmer and I am searching for a good algorithim >for finding one pattern in a source string... > It may not be a hashing function, but it does find a substring in a given string. Kind of a quick and dirty type utility one writes when needed. ----------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 */ -- -------------------------------------------------------------------------------- "If it ain't broke, don't fix it," and certainly don't blame me. UUCP: {pitt,scooter,hal,cwjcc,aablue}!neoucom!sam INTERNET: sam@neoucom.UUCP Scott A. Mason, Coordinator of Systems Operations, NEOUCOM