home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.pascal:5325 alt.msdos.programmer:2351
- Newsgroups: comp.lang.pascal,alt.msdos.programmer
- Path: sparky!uunet!usc!sol.ctr.columbia.edu!ira.uka.de!fauern!dec16!dec8!schoof
- From: schoof@dec8.informatik.uni-wuerzburg.dbp.de (Jochen "Joscho" Schoof)
- Subject: Re: 3 questions
- Message-ID: <1992Sep11.105333.10698@informatik.uni-wuerzburg.de>
- Sender: news@informatik.uni-wuerzburg.de (USENET News account)
- Organization: University of Wuerzburg, Germany
- References: <18ot9rINN67t@matt.ksu.ksu.edu>
- Date: Fri, 11 Sep 1992 10:53:33 GMT
- Lines: 46
-
- From article <18ot9rINN67t@matt.ksu.ksu.edu>, by holland@matt.ksu.ksu.edu (Rich Holland):
- [some lines deleted]
- > Question 2:
- > -----------
- > In general, what's the most efficient (fastest) way to search for a small
- > string inside a larger one (or for a group of say 5 bytes in a group
- > of say 1000 bytes)?
- >
- > I'd thought of something like this:
- >
- > For i:=1 to Length(String2)-Length(String1) do
- > Begin
- > If String1=Copy(String2,i,Length(String1)) then {we found it}
- > { so quit for-loop }
- > Else { keep looking }
- > End;
- [some lines deleted]
-
- I think the better way is to check only occurancies of the substring's
- first letter like in:
-
- FOR i:=1 TO Length(String2)-Length(String1) DO BEGIN
- IF String2[i]=String1[1] THEN
- IF String1=Copy(String2,i,Length(String1)) THEN Found:=TRUE;
-
- I think you are using Turbo Pascal and therefore you should avoid the
- Copy-function where possible, because it is very slooooooooow. A good
- hint when looking for algorithms like this is the following book:
-
- Robert Sedgewick
- Algorithms
- Addison-Wesley
-
- It offers you lots of standard-algorithms in Pascal source. Special
- version for C and C++ are also available. The above algorithm is not
- from that book, but the first idea I got, so I'm sure it's not most
- effective.
- Hope I helped you
-
- - Jochen
-
- --
- ---------------------------------------------------------------------------
- Jochen Schoof schoof@sundae.informatik.uni-wuerzburg.de
- Lehrstuhl f. Informatik II +-----------------------------------------------
- Universitaet Wuerzburg | It's worse than that: he's dead, Jim! -Bones
-