Results 1 to 10 of 10

Thread: The Palindrome

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    10

    Question

    I would like to know what code i would need , or an example of code for the following.

    i would like to enter say ten names in a text file,

    then i want the program to check if a word is a Palindrome or not.

    any idea's how to use instr to do this ... your help would be appreciated


    ps : a Palindrome is a word that is spelt backwards and forwards the same , eg : WayaW

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    small bit of help

    Here you are. It just looks at the contents of a text box to see if it is a palindrome or not.

    You should be able to adapt it easily to your needs.

    Code:
    Private Sub Command1_Click()
      Dim myText As String
      Dim c As Integer
      myText = Text1.Text
       
      If Len(myText) Mod 2 <> 1 Then
        MsgBox myText & " is not a Palindrome"
      Else
        For c = 1 To Len(myText) / 2
          If Mid(myText, c, 1) <> Mid(myText, Len(myText) - c + 1, 1) Then
            MsgBox myText & " is not a Palindrome"
            Exit Sub
          End If
        Next
        MsgBox myText & " is a Palindrome"
    
      End If
    End Sub
    Paul Lewis

  3. #3
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,628
    Sheesh! Paul beat me to it! But since I worked so hard on it, I will post my answer anyway. Slap a command button and a listbox on your form, then copy and paste the following code for your form:

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    
        Dim arrWords(1 To 5) As String
        Dim intX             As Integer
        
        arrWords(1) = "radar"
        arrWords(2) = "Naomi, did I moan?"
        arrWords(3) = "Visual Basic"
        arrWords(4) = "comp-u-geek"
        arrWords(5) = "Madam, I'm Adam"
        
        List1.Clear
        For intX = 1 To 5
            If IsPalindrome(arrWords(intX)) Then
                List1.AddItem Chr$(34) & arrWords(intX) & Chr$(34) _
                            & " is a palindrome."
            Else
                List1.AddItem Chr$(34) & arrWords(intX) & Chr$(34) _
                            & " is not a palindrome."
            End If
        Next
    
    End Sub
    
    
    
    Public Function IsPalindrome(strWord As String) As Boolean
    
        Dim strStrippedWord As String
        Dim strTestChar     As String * 1
        Dim intPosLeft      As Integer
        Dim intPosRight     As Integer
        Dim intLen          As Integer
        Dim intX            As Integer
        Const conAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
        
        ' remove all non-letters from the original word
        intLen = Len(strWord)
        strStrippedWord = ""
        
        For intX = 1 To intLen
            strTestChar = Mid$(strWord, intX, 1)
            If InStr(conAlphabet, strTestChar) > 0 Then
                strStrippedWord = strStrippedWord & strTestChar
            End If
        Next
        
        'check to see if its a palindrome
        
        IsPalindrome = True     'innocent until proven guilty
        
        intPosLeft = 1
        intPosRight = Len(strStrippedWord)
        
        ' convert all chars to uppercase
        strStrippedWord = UCase$(strStrippedWord)
    
        Do Until intPosLeft >= intPosRight
            If Mid$(strStrippedWord, intPosLeft, 1) _
             = Mid$(strStrippedWord, intPosRight, 1) Then
                intPosLeft = intPosLeft + 1
                intPosRight = intPosRight - 1
            Else
                IsPalindrome = False
                Exit Do
            End If
        Loop
        
    End Function
    "It's cold gin time again ..."

    Check out my website here.

  4. #4
    Addicted Member
    Join Date
    Feb 2000
    Location
    London, UK
    Posts
    145
    PaulLewis:
    Sorry to complain on you, but your method doesn't work on wards like 'anna' or 'tinnit' (OK, it's not a real word, not even in Swedish, but still...)
    Cannot a palindrome have an even number of digits?
    I shall see if I can find a way round it.
    Cheers,
    Pentax
    Wilhelm Tunemyr,
    Swede in London

    wilhelm.tunemyr@swipnet.se

    "Dort, wo man Bücher verbrennt, verbrennt man am Ende auch Menschen"
    Heinrich Heine (1797-1856)

    Pravda vítezi!
    (Truth prevails!)

  5. #5
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    True

    I did not realise that it was allowed to have an even number of letters.

    So just take away the first test and you'll be fine. Sorry for my confusion

    Code:
    Private Sub Command1_Click()
      Dim myText As String
      Dim c As Integer
      myText = Text1.Text
       
      For c = 1 To Len(myText) / 2
        If Mid(myText, c, 1) <> Mid(myText, Len(myText) - c + 1, 1) Then
          MsgBox myText & " is not a Palindrome"
          Exit Sub
        End If
      Next
      MsgBox myText & " is a Palindrome"
    End Sub
    Cheers
    Paul Lewis

  6. #6
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845

    Thumbs up

    BruceG
    That's a pretty neat solution!

    I like this bit:

    If InStr(conAlphabet, strTestChar) > 0 Then

    VERY neat!

    I usually do something using Asc(strTestChar)
    Mark
    -------------------

  7. #7
    Guest
    For VB 6 try this code:
    Note: It is NOT case-sensitive. You could remove the UCASE statements to make it case-sensitive.

    Code:
    Public Function IsPalinDrome(ByRef S As String)
         IsPalinDrome = UCase(S) = UCase(StrReverse(S))
    End Function
    To Test the code try this:

    Code:
    Private Sub Form_Load()
        Debug.Print IsPalinDrome("Test")
        Debug.Print IsPalinDrome("A name")
        Debug.Print IsPalinDrome("Wayaw")
        Debug.Print IsPalinDrome("Madam")
        Debug.Print IsPalinDrome("Radar")
    End Sub
    Enjoy!

  8. #8
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    for a phrase to be a Palindrome punctuation is ignored.

    RobIII'a and PaulLewis's solutions are therefore incomplete whereas BruceG's is spot on!

    [Edited by Mark Sreeves on 10-12-2000 at 10:34 AM]
    Mark
    -------------------

  9. #9
    Guest
    i want the program to check if a word is a Palindrome or not.
    My code is just the shortest and easiest way to check for a palindrome word as you can see. I never said it was for complete phrases. But I must admit you're right a teeny weeny little bit...

    [Edited by RobIII on 10-12-2000 at 10:46 AM]

  10. #10
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Don't underestimate the value...

    Mark Sreeves:

    Don't underestimate the value of delivering solutions that
    do what the customer asks for. It may be nice to go the
    extra mile and make it do even more, but in the real world,
    customers seldom enjoy paying for software that does more
    than they originally request.

    This is not a defensive statement regarding your post at
    all. Just a comment to any aspiring programmers who plan
    one day to program for a living.

    For most of the short term contracts I've been involved in,
    the key requirements were: on-time delivery, delivery of
    requirements, ease of use, reliability, documentation and
    testing. Never have I had a disappointed customer due to
    failure to deliver more than what was asked.

    Having said that, I must agree that for any word including
    punctuation (apostrophe or hyphen for example), the code I
    did which seemed to me to be the simplest way of checking,
    would not be complete.

    To change the code, I would simply replace all characters
    in the string that were not a-z or A-Z with "". Then do
    the same test as I already do.

    Regards
    Paul Lewis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width