Re: Is it a Palindrome!!!
Think about how you'd do it in "real life", take a piece of paper, and take notes.
When you've got a list, point by point, of how you'd manually check if a string is a palindrome, try to convert it to code.
Re: Is it a Palindrome!!!
Just check the first character and compare with the last one, the second with the one before the last... and go until you pass the middle of the string length. Need at the beginning of the test check if it's a par length or not...
Re: Is it a Palindrome!!!
Alternatively, get the length and do an integer divide by 2 (\ not /). Then have a List(of String). Walk through the string from the first character to Length\2, adding each character to the List. Then walk backwards from the end of the string (Length-1), checking each character.
After thinking about it some, this is the same as what mickey_pt suggested....only less efficient.
Re: Is it a Palindrome!!!
Quote:
Originally Posted by
mickey_pt
Need at the beginning of the test check if it's a par length or not...
Hm, I don't think you have to do that. You only need a loop that stops when it passes the middle of the string.
You only need to check if the string length is 2 or higher.
Re: Is it a Palindrome!!!
you could also store the reverse string. Then, you could compare the matching indexes of each string. might be less confusing to code (for me anyway) and would involve less "math" trying to calculate the characters' index. this will also save you from having to check the string's length.
Example: "states"
String1 holds: states
String2 holds: setats
compare string1's index 0 to string2's index 0. match? check
compare string1's index 1 to string2's index 1. match? no. msgbox(not a palindrome)
Re: Is it a Palindrome!!!
@stateofidleness He wants to do it character by character and doesn't want to use any reverse functions.
Re: Is it a Palindrome!!!
he will be checking it character by character. the reverse string part will only store the word in reverse.
I know he didn't want code, but something like:
vb Code:
Private Sub checkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkButton.Click
Dim BeginString() As Char = myStringTextbox.Text.ToCharArray
Dim ReverseString() As Char = myStringTextbox.Text.ToCharArray
Array.Reverse(ReverseString)
Dim i As Integer
For i = 0 To BeginString.Length - 1
If BeginString(i).ToString = ReverseString(i).ToString Then
MsgBox("so far so good")
Else
MsgBox("definitely -NOT- a palindrome")
Exit Sub
End If
Next
End Sub
he'd still be going char by char, he'd just have two strings instead of 1
Re: Is it a Palindrome!!!
something no one has seemed to mention, but I'll bring it up, just so that it's out there: before any comparison can be done ALL PUNCTUATION and ALL SPACES need to be removed from the string first. the fasted way to do it is to compare the Index character to the (Length-1)-Index character... if you find a mismatch, it's not a palindrome, and you can exit the loop. As soon as Index => Length /2 ... then you have a palindrome...
-tg
Re: Is it a Palindrome!!!
Quote:
Originally Posted by
stateofidleness
he will be checking it character by character. the reverse string part will only store the word in reverse.
I know he didn't want code, but something like:
vb Code:
Dim BeginString() As Char = yourString.ToCharArray
Dim ReverseString() As Char = yourString.ToCharArray
Array.Reverse(ReverseString)
Dim i as integer
for i = 0 to BeginString.Length
'compare and do work
next i
he'd still be going char by char, he'd just have two strings instead of 1
Yes but:
Quote:
I dont want to use any reverse string function......
Re: Is it a Palindrome!!!
*sigh* ... that's what I get for not checking for replies before I post...
State - that solution still uses a reverse function...
Code:
Dim myString As String = "MadamImAdam" 'Assuming I've already eliminated spaces and punctuation
Dim myCounter as Integer = myString.Length / 2
Dim isPalendrome As Boolean = True
For x As Integer = 0 to myCounter
if myString.subString(x,1).ToLower <> myString.SubString((myString.Length-1) - x, 1).ToLower Then
isPalendrome = False
Exit for
End If
Next
It's short, compact, simple and character by character...
-tg
Re: Is it a Palindrome!!!
ahh very cool tech. I like that!
Re: Is it a Palindrome!!!
I don't know why you are reluctant to use any string reverse function.
It is just one line code using that. (No need to loop etc.)
vb Code:
Private Function IsPalindrome(ByVal input As String)
Return input.StartsWith(Strings.StrReverse(input.Substring(input.Length \ 2)))
End Function
Re: Is it a Palindrome!!!
Here is the loop version that doesn't use any string reverse function as you need:
vb Code:
Private Function IsPalindrome(ByVal input As String)
For i As Integer = 0 To input.Length \ 2
If 0 <> String.Compare(input.Chars(i), input.Chars(input.Length - 1 - i), True) Then Return False
Next
Return True
End Function
Re: Is it a Palindrome!!!
C'mon guys, he said no code and no reverse functions...
Re: Is it a Palindrome!!!
well when the thread takes on a life of its own and the OP isn't around to respond, thins kind of thing happens. Odds are this was an assignment... which might be why there he can't use any reverse functions. Who knows?
The steps are pretty simple though...
compare first letter with the last... keep working towards the middle until you either a)Hit the middle (Palindrome = True, or b) find a mismatch (Palindrome = false).
That's really all there is to it.
and is exactly what Mickey suggested in post #3.
-tg