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


Composition to search external text files


function search( pat: PATTERN; text: TEXT ): integer; var i, j, m, n: integer; found: boolean; begin found := FALSE; search := 0; m := length(pat); if m=0 then begin search := 1; found := TRUE; end; n := length(text); j := 1; i := 1; while (i<=n) and not found 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; found := TRUE; end end; i := i + 1; end end;

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



© Addison-Wesley Publishing Co. Inc.