I am making an anagram solver.
I have written a function to solve the anagram, with two parameters; "Anagram" (the scrambled string) and "Dictionary" (the arraylist of words).
I am confused! Sometimes it solves it correctly, 99% of the time, it doesn't - im not sure where I have gone wrong!
Here is my code:
Private Function SolveAnagram(ByVal Anagram As String, ByVal Dictionary As ArrayList)
Dim solved As String = ""
Dim inputarray As Array = Anagram.ToCharArray
Dim charcount As Integer = Anagram.Length
Dim check As Integer = 0
Dim exiting As Boolean = False
For i As Integer = 0 To Dictionary.Count - 1
If Dictionary.Item(i).ToString.Length = charcount Then
For Each character As Char In inputarray
If Dictionary.Item(i).ToString.Contains(character) Then
check += 1
Else
Exit For
End If
Next
If check = charcount Then
solved = Dictionary.Item(i).ToString
exiting = True
End If
End If
If exiting = True Then
Exit For
End If
Next
If solved = "" Then
MsgBox("Failed to solve anagram", MsgBoxStyle.Exclamation)
End If
Return solved
End Function
Thanks in advance!
I have written a function to solve the anagram, with two parameters; "Anagram" (the scrambled string) and "Dictionary" (the arraylist of words).
I am confused! Sometimes it solves it correctly, 99% of the time, it doesn't - im not sure where I have gone wrong!
Here is my code:
Private Function SolveAnagram(ByVal Anagram As String, ByVal Dictionary As ArrayList)
Dim solved As String = ""
Dim inputarray As Array = Anagram.ToCharArray
Dim charcount As Integer = Anagram.Length
Dim check As Integer = 0
Dim exiting As Boolean = False
For i As Integer = 0 To Dictionary.Count - 1
If Dictionary.Item(i).ToString.Length = charcount Then
For Each character As Char In inputarray
If Dictionary.Item(i).ToString.Contains(character) Then
check += 1
Else
Exit For
End If
Next
If check = charcount Then
solved = Dictionary.Item(i).ToString
exiting = True
End If
End If
If exiting = True Then
Exit For
End If
Next
If solved = "" Then
MsgBox("Failed to solve anagram", MsgBoxStyle.Exclamation)
End If
Return solved
End Function
Thanks in advance!