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


Brute force text searching with k mismatches


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

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



© Addison-Wesley Publishing Co. Inc.