Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Palindrome help.

Palindrome help.

Scheduled Pinned Locked Moved Visual Basic
helpcss
11 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BigGreen
    wrote on last edited by
    #1

    Just starting out in programming, and I'm trying to create a program that can tell the user whether or not the word they input is a palindrome regardless of the length of the word. Here's what I have as far as code: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim a, b As String Dim length As Integer a = InputBox("Enter a word to check and see if it is a Palindrome.") length = Len(a) If a.Substring(0, 1) = a.Substring(length, -1) Then MsgBox("This is a Palindrome") Else MsgBox("This is not a Palindrome") End If End Sub I get an error stateing "Additional information: Length cannot be less than zero." Any insight into this matter would be greatly appreciated P.S. Just for the record, I'm not looking for the answer. A point in the right direction would help though ;)

    M J L Q 4 Replies Last reply
    0
    • B BigGreen

      Just starting out in programming, and I'm trying to create a program that can tell the user whether or not the word they input is a palindrome regardless of the length of the word. Here's what I have as far as code: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim a, b As String Dim length As Integer a = InputBox("Enter a word to check and see if it is a Palindrome.") length = Len(a) If a.Substring(0, 1) = a.Substring(length, -1) Then MsgBox("This is a Palindrome") Else MsgBox("This is not a Palindrome") End If End Sub I get an error stateing "Additional information: Length cannot be less than zero." Any insight into this matter would be greatly appreciated P.S. Just for the record, I'm not looking for the answer. A point in the right direction would help though ;)

      M Offline
      M Offline
      MarkC
      wrote on last edited by
      #2

      BigGreen wrote: If a.Substring(0, 1) = a.Substring(length, -1) Then I'm in no way a VB dude, but is the comparison operator supposed to be: "=="? As in: If a.Substring(0, 1) **==** a.Substring(length, -1) Then Regards, MarkC# --------- .Net Head

      I 1 Reply Last reply
      0
      • M MarkC

        BigGreen wrote: If a.Substring(0, 1) = a.Substring(length, -1) Then I'm in no way a VB dude, but is the comparison operator supposed to be: "=="? As in: If a.Substring(0, 1) **==** a.Substring(length, -1) Then Regards, MarkC# --------- .Net Head

        I Offline
        I Offline
        Ian Darling
        wrote on last edited by
        #3

        MarkC# wrote: I'm in no way a VB dude, but is the comparison operator supposed to be: "=="? Not in any dialect of VB I know of. Equality *and* assignment are both '=' in VB. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

        T M 2 Replies Last reply
        0
        • B BigGreen

          Just starting out in programming, and I'm trying to create a program that can tell the user whether or not the word they input is a palindrome regardless of the length of the word. Here's what I have as far as code: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim a, b As String Dim length As Integer a = InputBox("Enter a word to check and see if it is a Palindrome.") length = Len(a) If a.Substring(0, 1) = a.Substring(length, -1) Then MsgBox("This is a Palindrome") Else MsgBox("This is not a Palindrome") End If End Sub I get an error stateing "Additional information: Length cannot be less than zero." Any insight into this matter would be greatly appreciated P.S. Just for the record, I'm not looking for the answer. A point in the right direction would help though ;)

          J Offline
          J Offline
          Jeff Varszegi
          wrote on last edited by
          #4

          First, I would compare characters using the Chars property. Creating a new string (which Substring presumably does) just to check a character value is a little wasteful, although nothing serious in your case. The thing that's throwing the error is a.Substring(length, -1) (I think-- I'm not a VB.NET coder). Your task is to check every character against the corresponding one the same distance from the end of the string. This means you will have to use a loop (even a recursive function call will still use 'tail recursion', which should be considered a loop, right?). Why is this? Because you can't tell beforehand how many things you'll have to check! Read the documentation on the for loop; it's the most appropriate loop type in VB for your purpose. For extra fun, you should try reading about loop unrolling on the Web; then read about Duff's device. Also, you need to start thinking even early on about always abstracting your code. Checking a string for palindromeness seems to be a useful function, which you may be able to reuse later; it's also a well-defined small problem. It's appropriate to make it into a function. I'm appending a solution; don't read it unless you want to. Regards, Jeff Varszegi

          J 1 Reply Last reply
          0
          • J Jeff Varszegi

            First, I would compare characters using the Chars property. Creating a new string (which Substring presumably does) just to check a character value is a little wasteful, although nothing serious in your case. The thing that's throwing the error is a.Substring(length, -1) (I think-- I'm not a VB.NET coder). Your task is to check every character against the corresponding one the same distance from the end of the string. This means you will have to use a loop (even a recursive function call will still use 'tail recursion', which should be considered a loop, right?). Why is this? Because you can't tell beforehand how many things you'll have to check! Read the documentation on the for loop; it's the most appropriate loop type in VB for your purpose. For extra fun, you should try reading about loop unrolling on the Web; then read about Duff's device. Also, you need to start thinking even early on about always abstracting your code. Checking a string for palindromeness seems to be a useful function, which you may be able to reuse later; it's also a well-defined small problem. It's appropriate to make it into a function. I'm appending a solution; don't read it unless you want to. Regards, Jeff Varszegi

            J Offline
            J Offline
            Jeff Varszegi
            wrote on last edited by
            #5
            Function IsPalindrome(ByVal s As String) As Boolean
                Dim length As Integer
                Dim position As Integer
            
                length = s.Length
            
                ' You only have to check half the string against the other half
                '
                ' You might ask, What about the middle character?
                ' For an odd number of characters, the center character will not
                ' be checked because the remainder is discarded in integer division.
                ' That is all right, though, because the center char can be anything
                ' without destroying the property of palindromeness.
                For position = 0 To (length / 2)
                    If (s.Chars(position) <> s.Chars((length - position) - 1)) Then
                        Return False  ' This immediately exits the function
                    End If
                Next position
            
                Return True
            End Function
            

            Regards, Jeff Varszegi

            B 1 Reply Last reply
            0
            • J Jeff Varszegi
              Function IsPalindrome(ByVal s As String) As Boolean
                  Dim length As Integer
                  Dim position As Integer
              
                  length = s.Length
              
                  ' You only have to check half the string against the other half
                  '
                  ' You might ask, What about the middle character?
                  ' For an odd number of characters, the center character will not
                  ' be checked because the remainder is discarded in integer division.
                  ' That is all right, though, because the center char can be anything
                  ' without destroying the property of palindromeness.
                  For position = 0 To (length / 2)
                      If (s.Chars(position) <> s.Chars((length - position) - 1)) Then
                          Return False  ' This immediately exits the function
                      End If
                  Next position
              
                  Return True
              End Function
              

              Regards, Jeff Varszegi

              B Offline
              B Offline
              BigGreen
              wrote on last edited by
              #6

              Thanx for all the input ya'll. This is the solution that I finally came up with. I've never created my own function so, that was the part that got me. This was actually in the school book (just the function not the code) in next weeks lesson area. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim a As String Dim b As Integer = 1 Do While b = 1 a = InputBox("Enter a word to find out if it's a Palindrome.") If reverse(a) = a Then MsgBox("This word is a palindrome.") Else MsgBox("This word is not a palindrome.") End If If a = "z" Then b = b + 1 End If Loop End Sub Function reverse(ByVal info As String) As String Dim m, j As Integer, temp As String = "" m = info.Length For j = m - 1 To 0 Step -1 temp &= info.Substring(j, 1) Next Return temp End Function A lot of this is not really necessary however, I figured it was a good chance for a lil practice. Thanx again for all the input :D P.S. Sorry about the Lounge post that was my second post here and I was in a rush so didn't read the rules :P won't happen again ;)

              E 1 Reply Last reply
              0
              • B BigGreen

                Thanx for all the input ya'll. This is the solution that I finally came up with. I've never created my own function so, that was the part that got me. This was actually in the school book (just the function not the code) in next weeks lesson area. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim a As String Dim b As Integer = 1 Do While b = 1 a = InputBox("Enter a word to find out if it's a Palindrome.") If reverse(a) = a Then MsgBox("This word is a palindrome.") Else MsgBox("This word is not a palindrome.") End If If a = "z" Then b = b + 1 End If Loop End Sub Function reverse(ByVal info As String) As String Dim m, j As Integer, temp As String = "" m = info.Length For j = m - 1 To 0 Step -1 temp &= info.Substring(j, 1) Next Return temp End Function A lot of this is not really necessary however, I figured it was a good chance for a lil practice. Thanx again for all the input :D P.S. Sorry about the Lounge post that was my second post here and I was in a rush so didn't read the rules :P won't happen again ;)

                E Offline
                E Offline
                Edbert P
                wrote on last edited by
                #7

                Interesting, two different solutions for the same thing. I think reversing the word and checking whether it's the same as the original word is the easiest, which reminds me to always look at a problem from a different point of view :-D.

                1 Reply Last reply
                0
                • I Ian Darling

                  MarkC# wrote: I'm in no way a VB dude, but is the comparison operator supposed to be: "=="? Not in any dialect of VB I know of. Equality *and* assignment are both '=' in VB. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

                  T Offline
                  T Offline
                  the_warlord
                  wrote on last edited by
                  #8

                  '==' is C# only.

                  1 Reply Last reply
                  0
                  • B BigGreen

                    Just starting out in programming, and I'm trying to create a program that can tell the user whether or not the word they input is a palindrome regardless of the length of the word. Here's what I have as far as code: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim a, b As String Dim length As Integer a = InputBox("Enter a word to check and see if it is a Palindrome.") length = Len(a) If a.Substring(0, 1) = a.Substring(length, -1) Then MsgBox("This is a Palindrome") Else MsgBox("This is not a Palindrome") End If End Sub I get an error stateing "Additional information: Length cannot be less than zero." Any insight into this matter would be greatly appreciated P.S. Just for the record, I'm not looking for the answer. A point in the right direction would help though ;)

                    L Offline
                    L Offline
                    LaptopBoy311
                    wrote on last edited by
                    #9

                    An issue you may be running into is your Dim statement. I'm not sure if you're using 6.0 or .NET (and I've yet to touch .NET), but with 6.0 you have to have a declaration type with each variable. For example, Dim a as String, b as String instead of Dim a, b as String The way you have it now, a is declared as a Variant which is basically a catch all variable type and extremely inefficient :mad:. I avoid them like I would a FORMAT C:. Since I avoid playing around with them, I'm not sure about their little quirks and personalities, however, you're performing a lot of functions that work best with strings. What is b used for? I don't see it in your code at all except at Dim. -------------------- Tim Perry "Remember, comments are there to make people laugh, not to help them fix your code later...because you're code is always perfect"

                    1 Reply Last reply
                    0
                    • B BigGreen

                      Just starting out in programming, and I'm trying to create a program that can tell the user whether or not the word they input is a palindrome regardless of the length of the word. Here's what I have as far as code: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim a, b As String Dim length As Integer a = InputBox("Enter a word to check and see if it is a Palindrome.") length = Len(a) If a.Substring(0, 1) = a.Substring(length, -1) Then MsgBox("This is a Palindrome") Else MsgBox("This is not a Palindrome") End If End Sub I get an error stateing "Additional information: Length cannot be less than zero." Any insight into this matter would be greatly appreciated P.S. Just for the record, I'm not looking for the answer. A point in the right direction would help though ;)

                      Q Offline
                      Q Offline
                      Quatl
                      wrote on last edited by
                      #10

                      i believe the code you want is: if a.substring(0,1)=a.substring(length-1,1) rather than if a.substring(0,1)=a.substring(length, -1)

                      1 Reply Last reply
                      0
                      • I Ian Darling

                        MarkC# wrote: I'm in no way a VB dude, but is the comparison operator supposed to be: "=="? Not in any dialect of VB I know of. Equality *and* assignment are both '=' in VB. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

                        M Offline
                        M Offline
                        MarkC
                        wrote on last edited by
                        #11

                        Ian Darling wrote: Equality *and* assignment are both '=' in VB. There ya go. At least I can say with full cliche in tow, "I learn something new every day." I'll stick with C# and Java, thank you. Best wishes! :-O Regards, MarkC# --------- .Net Head

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups