VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 306,811 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,692 people online right now. Registration is fast and FREE... Join Now!




Compare Text and count differences based on characters

 

Compare Text and count differences based on characters, A typing test program, where typed text is matched with a parent text

nadiya_hafis

15 Feb, 2009 - 09:21 PM
Post #1

New D.I.C Head
*

Joined: 11 Feb, 2009
Posts: 9

I have two multiline textboxes, and I want to compare and count the text differences between them. The algorithm appears very complex.. To be exact, I need to check for the spelling mistakes (spaces and newlines can be ignored..)Can anyone please help me with this??

User is offlineProfile CardPM
+Quote Post


MrWobbles

RE: Compare Text And Count Differences Based On Characters

16 Feb, 2009 - 06:39 AM
Post #2

D.I.C Head
Group Icon

Joined: 11 Apr, 2008
Posts: 229



Thanked: 10 times
Dream Kudos: 25
My Contributions
I should not give you any code, because you did not post any, but I can help you by referring you to Google, or Yahoo or some other similar search engine - surely this has been done before. Try something like this:

http://www.google.com/search?hl=en&q=s...amp;btnG=Search
User is offlineProfile CardPM
+Quote Post

nadiya_hafis

RE: Compare Text And Count Differences Based On Characters

19 Feb, 2009 - 12:58 AM
Post #3

New D.I.C Head
*

Joined: 11 Feb, 2009
Posts: 9

Well, I did try , but couldn't come up with what I wanted..I need to compare two text files and count the character differences between them..like in a typing test program.The code I came up with just checks if a word exists in the parent file, (irrespective of the position). Please suggest the changes, I need to make to my current code..

Here's the code:
CODE

      Dim sr As StreamReader
        Dim strFile1, strFile2 As String
        Dim strFile1Lines(), strFile2Lines() As String
        Dim strFile1NotFile2 As String = ""
        Dim strFile2NotFile1 As String = ""


        ' Open File1
        sr = New StreamReader(file1)
        strFile1 = sr.ReadToEnd()
        sr.Close()

        'Open File2
        sr = New StreamReader(file2)
        strFile2 = sr.ReadToEnd()
        sr.Close()

        'delimiter for splitting strings
        Dim delim() As String = {" ", vbCrLf}

        ' Seperate the contents of File1 and File2 into words
strFile1Lines = strFile1.Split(delim, StringSplitOptions.RemoveEmptyEntries)
strFile2Lines = strFile2.Split(delim, StringSplitOptions.RemoveEmptyEntries)

        ' Going through each word in File 1
        For Each strFile1Line As String In strFile1Lines
            Dim bLineInFile2 As Boolean = False

            ' Compare the current word in File1 against all lines in File2
            For Each strFile2Line As String In strFile2Lines
                If strFile1Line = strFile2Line Then bLineInFile2 = True
            Next

            ' If the word is in not in File2 add it to the variable strFile1NotFile2
            If Not bLineInFile2 Then strFile1NotFile2 = strFile1NotFile2 & strFile1Line & vbCrLf
        Next

    'display the unmatched words in a richtextbox
        RichTextBox1.Text = strFile1NotFile2


User is offlineProfile CardPM
+Quote Post

n8wxs

RE: Compare Text And Count Differences Based On Characters

19 Feb, 2009 - 03:01 AM
Post #4

--... ...-- -.. . -. ---.. .-- -..- ...
Group Icon

Joined: 6 Jan, 2008
Posts: 1,609



Thanked: 230 times
My Contributions
You might try removing the whitespaces and newlines from the strings you create by reading the files, rather than splitting them. See String.Replace Method

CODE

,,,
strFile1  = strFile1.Replace(" "c, "") //' remove all spaces
...
strFile2  = strFile2.Replace(" "c, "") //' remove all spaces
...
if strFile2 <> strFile1 then
    //' complain
end if
...
For i As Integer = 0 To strFile1.Length - 1
    If strFile1(i) <> strFile2(i) Then
        MessageBox.Show(strFile1(i) & " <> " & strFile2(i))
    End If
Next
...

User is offlineProfile CardPM
+Quote Post

nadiya_hafis

RE: Compare Text And Count Differences Based On Characters

19 Feb, 2009 - 03:39 AM
Post #5

New D.I.C Head
*

Joined: 11 Feb, 2009
Posts: 9

Thanks, but that isn't helping me..
Your code compares characters at equal positions..Ex:

Original Text: I have a doll
Typed Text: I have doll

The number of typing mistakes : 1, but using u'r code, we get total no. of typing mistakes as 4. And mine shows Mistakes=0(because there is no spelling mistakes in the typed words )
I'm trying to develop something similar to the 'Compare Document' function of MS Word, where two text are compared on character level.
Is this possible??
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 09:54PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month