Palindrome help.
-
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 ;)
-
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 HeadMarkC# 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
-
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 ;)
First, I would compare characters using the
Chars
property. Creating a new string (whichSubstring
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 isa.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 thefor
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 -
First, I would compare characters using the
Chars
property. Creating a new string (whichSubstring
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 isa.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 thefor
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 VarszegiFunction 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
-
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
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 ;)
-
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 ;)
-
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
'==' is C# only.
-
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 ;)
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"
-
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 ;)
-
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