Palindrome code for Visual Basic 5.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xlilxmizzxinnocentx
    New Member
    • Jul 2007
    • 1

    Palindrome code for Visual Basic 5.0

    Hiya
    I was woundering if anyone could help me. A few weeks ago i started using vb 5.0 and now im trying to make a code to determine if a word is a palindrome or not. The code that i have tried dose not work and i was woundering if any one could help.
    Thanks

    Private Sub cmdStart_Click( )

    Dim inputWord As String
    Dim reverseWord As String

    inputWord = InputBox("Pleas e enter your word")

    If inputWord = reverseWord Then

    MsgBox "Yes, it's a palindrome"

    Else

    MsgBox "No, it's not a palindrome"

    End If

    End Sub
  • mbailey
    New Member
    • Jul 2007
    • 3

    #2
    Your first problem is that you do not set reverseWord to anything in your code. You can also make your program a bit more robust by eliminating whitespace and case from the comparison. That will allow multiword palindromes like "Oozy rat in a sanitary zoo" to be tested. I'm not familiar with VB 5.0 and VB.Net has a lot of built-in functionality to do this but the following code should work (I haven't tested it and, if VB 5 uses 1 based indexing, you'll need to change the indexing in the loops):

    Code:
    Private Sub cmdStart_Click()
       Dim inputWord As String
       Dim testWord As String
       Dim reverseWord As String
       Dim testChar as String
       Dim i as Integer = 0
       Dim maxIndex as Integer = 0
    
       '--Get input from the user.
       inputWord = InputBox("Please enter your word")
    
       '--Record the input in lower case and remove spaces (you can
       '--add checks for tabs, linefeeds, etc).
       maxIndex = Len(inputWord) - 1
       for i = 0 to maxIndex
          testChar = Mid(inputWord, i, 1)
          if testChar <> " " then
             testWord = testWord & LCase(testChar)
          end if
       next
    
       '--Reverse testWord.
       maxIndex = Len(testWord) - 1
       for i = maxIndex to 0 Step -1
          reverseWord = reverseWord & Mid(testWord, i, 1)
       next
    
       '--Compare reverseWord to testWord and output the results.
       If testWord = reverseWord Then
          MsgBox "Yes, it's a palindrome"
       Else
          MsgBox "No, it's not a palindrome"
       End If
    End Sub
    Good luck,
    Michael Bailey

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by xlilxmizzxinnoc entx
      I was woundering if anyone could help me. A few weeks ago i started using vb 5.0 and now im trying to make a code to determine if a word is a palindrome or not. The code that i have tried dose not work and i was woundering if any one could help.
      Not sure whether it existed in VB version 5, but here's a quote from the VBA documentation.. .

      the StrReverse Function

      Description

      Returns a string in which the character order of a specified string is reversed.

      Comment

      Working...