Anagrams and Palindromes
More Java homework:

Dim vaOne As Variant
Dim vaTwo As Variant
Dim bReturn As Boolean
Dim i As Long
Const sRPLC As String = "+"
sWordOne = Replace(sWordOne, Space(1), vbNullString)
sWordTwo = Replace(sWordTwo, Space(1), vbNullString)
If Len(sWordOne) = Len(sWordTwo) Then
For i = 1 To Len(sWordOne)
sWordTwo = Replace(sWordTwo, Mid$(sWordOne, i, 1), sRPLC, 1, 1, vbTextCompare)
Next i
bReturn = sWordTwo = String(Len(sWordOne), sRPLC)
Else
bReturn = False
End If
IsAnagram = bReturn
End Function
First, I remove all the spaces. Then I make sure the two words are the same length. Then I loop through all the letters in the first word, find them in the second word, and replace them with a plus sign. If the second word is all plus signs at the end, then it’s an anagram. My first thought was to put the letters in an array and sort them, but that’s too much looping.
Dim i As Long
Dim bReturn As Boolean
bReturn = True
sPhrase = Replace(sPhrase, Space(1), vbNullString)
For i = 1 To Len(sPhrase)
If LCase(Mid$(sPhrase, i, 1)) <> LCase(Mid$(sPhrase, Len(sPhrase) + 1 – i, 1)) Then
bReturn = False
Exit For
End If
Next i
IsPalindrome = bReturn
End Function
Nothing too fancy here. Again, I remove all the spaces. Then I compare the first letter to the last letter, the second letter to the penultimate letter, and so on. If there’s every not a match, set the return value to False and quit looking.




