1,105,375 Community Members

Palindrome Checking

Member Avatar
Aurax
Newbie Poster
10 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi all

I am trying to write a program to recognizing word or phrase is a palindrome or not.
Please help me

Regards
Aura

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

Use StrReverse() function to reserve the word/phrase. Compare the real words/phrase with reserved words/phrase.
Check out the result if it same or not.

Private Sub Command1_Click()
    Dim Palindrome As String

    Palindrome = StrReverse(Text1.Text)

    If StrComp(Text1.Text, Palindrome, vbTextCompare) = 0 Then
        MsgBox "Palindrome!"
    Else
        MsgBox "Not a Palindrome!"
    End If
End Sub
Member Avatar
Reverend Jim
Noli mentula
5,445 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

But before you reverse the string you should normalize it.

  1. convert everything to upper or lower case
  2. remove all non-letters

If you were to check the phrase "Able was I ere I saw Elba" without normalizing it would not be flagged as a palindrome. After normalizing it would be "ablewasiereisawelba" which would be flagged as a palindrome.

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

If you were to check the phrase "Able was I ere I saw Elba" without normalizing it would not be flagged as a palindrome. After normalizing it would be "ablewasiereisawelba" which would be flagged as a palindrome.

Nope. It also recognized as palindrome. StrReverse will completed reverse the words included the space.

Member Avatar
scudzilla
Posting Whiz
380 posts since Mar 2007
Reputation Points: 32 [?]
Q&As Helped to Solve: 41 [?]
Skill Endorsements: 1 [?]
 
0
 

Sir Jim's example would be flagged as palindrome because the spaces are symmetrical.

How about this one? "A nut for a jar of tuna". Without normalizing, it will not be flagged as one.

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

A nut for a jar of tuna

That makes sense.
@reverend jim and scidzilla : Thanks for correcting
@Aura : this following codes is modified with other members suggestion :

Function RemoveNonLetterChar(theString)
    strAlphaNumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    For i = 1 To Len(theString)
        strChar = Mid(theString, i, 1)
        If InStr(strAlphaNumeric, strChar) Then
            CleanedString = CleanedString & strChar
        End If
    Next
    RemoveNonLetterChar = CleanedString
End Function

Private Sub Command1_Click()
    Dim Temp, Palindrome As String

    Temp = LCase(RemoveNonLetterChar(Text1.Text)) 'lower case and remove non letters char
    Palindrome = StrReverse(Temp)

    If StrComp(Temp, Palindrome, vbTextCompare) = 0 Then
        MsgBox "Palindrome!"
    Else
        MsgBox "Not a Palindrome!"
    End If
End Sub
Member Avatar
Aurax
Newbie Poster
10 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

This is perfect.
Thank you all.

Question Answered as of 1 Year Ago by Jx_Man, Reverend Jim and scudzilla
Member Avatar
Reverend Jim
Noli mentula
5,445 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

Sir Jim's example would be flagged as palindrome because the spaces are symmetrical.

Only if you did the compare case-insensitive. But I suppose I could have picked a better example. Madam, I'm Adam would have been better. Or A man, a plan, a canal. Panama.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article