I would like to check to see if a string of words reads the same when read backwards. Like the words "eye, eve", etc. Any ideas on this? thanks.
Printable View
I would like to check to see if a string of words reads the same when read backwards. Like the words "eye, eve", etc. Any ideas on this? thanks.
Dim strPalindrome As String
Dim strStrip As String
Dim intIndex As Integer
strPalindrome = "A man a plan a canal Panama"
For intIndex = 1 To Len(strPalindrome)
If Mid$(strPalindrome, intIndex, 1) <> " " Then
strStrip = strStrip & Mid$(strPalindrome, intIndex, 1)
End If
Next
If UCase(strStrip) = StrReverse(UCase(strStrip)) Then
MsgBox "It's a palindrome"
End If
This should work:
By the way,I think the word is Palindrome and not Palidrome.Code:X = ""
L = LEN(Text1.Text)
FOR I = L TO 1 STEP -1
X = X + MID$(Text1.Text,I,1)
NEXT I
IF X = Text1.Text Then MsgBox "Palindrome"
rv2k's soultion won't work if there is more than one word and the words are different lengths (because of the spaces), but you can use his code to reverse the string instead of StrReverse if you don't have VB6.
Thanks Martin! That works fine.