CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Help!! Matching texts among text files

1. Junior Member
Join Date
Feb 2005
Posts
6

## Help!! Matching texts among text files

Hi,

I've read about matching strings and all but I still am not able to write. I would like to compare the strings in one text file with another text file, and then compare to see if there're any matches. I have the following codes, which is a string matching algorithm called Karp-Rabin, whereby a hashing function is used to check the resemblance between two words. So, instead of checking at each position of the text if the pattern occurs, it seems to be more efficient to check only if the contents of the window “looks like” the pattern.

'Function Rehash (note: d has to be supplied as well!)
Function Rehash(a As Integer, b As Integer, h As Integer, _
d As Integer) As Integer
Rehash = (h - a * d) * 2 + b
End Function

Sub KR(ByRef x() As Byte, ByVal m As Integer, ByRef y() As Byte, _
ByVal n As Integer)
'% stands for As Integer
Dim d%, hx%, hy%, i%, j As Integer

' /* Preprocessing */
' /* computes d = 2^(m-1) with
'the left-shift operator */
d = 2 ^ (m - 1)
i = m - 1
hx = i
hy = hx
For i = 0 To m - 1
hx = hx * 2 + x(i)
hy = hy * 2 + y(i)
Next

'/* Searching */
j = 0
Do While (j <= n-m)
'I am not really sure if I got this one right... Sure hope so
If (hx = hy And CompareArrays(x, y, j, m)) Then
'OUTPUT(j); '//Don't know what this does
End If
hy = Rehash(CInt(y(j)), CInt(y(j + m)), hy, d)
j = j + 1
Loop
End Sub

Function CompareArrays(x() As Byte, y() As Byte, j As Integer, _
m As Integer) As Boolean
Dim i As Integer
Dim bSame As Boolean

bSame = True
For i = j To m - 1
If (x(i) <> y(i + j)) Then
bSame = False
Exit For
End If
Next i

CompareArrays = bSame
End Function

I just dont know how to adapt this code to my program. I hope someone will be able to help me out.

2. Member +
Join Date
Jul 2003
Location
Florida
Posts
651

## Re: Help!! Matching texts among text files

Have you seen this post by cjard?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•