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


Composition to search external text files


program wc( input, output ); const MAXPATLEN = 5; const MAXTEXTLEN = 1000; type PATTERN = packed array [1..MAXPATLEN] of char; TEXT = packed array [1..MAXTEXTLEN] of char; var pat: PATTERN; text: TEXT; i: integer; function search( pat: PATTERN; text: TEXT ): integer; var i, j, m, n: integer; found: boolean; begin writeln('pat: ', pat, 'text: ', text ); found := FALSE; search := 0; m := length(pat); if m=0 then begin search := 1; found := TRUE; end; n := length(text); writeln( 'm = ', m, 'n = ', n ); 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; begin { while not eof(f) do begin read(pat); read(text); } i := search('aaaab','aaaaaaaaaaaaab'); writeln( 'pattern found at ', i ); { end; } end.

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



© Addison-Wesley Publishing Co. Inc.