[Home]
[Chapter]
[Contents]
[Previous Algorithm]
[Next Algorithm]


Brute force string searching


function search( pat: PATTERN; text: TEXT ): integer; var i, j, m, n: integer; begin search := 0; m := length(pat); if m=0 then begin search := 1; goto 999; end; n := length(text); j := 1; i := 1; while (i<=n) do begin if text[i] <> pat[j] then begin i := i - j + 1; j := 1; end else begin j := j + 1; if j > m then begin search := i - j + 2; goto 999; end end; i := i + 1; end 999: end;

C source (711a.srch.c) Pascal source (711a.srch.p)



© Addison-Wesley Publishing Co. Inc.