|
-
Aug 17th, 2009, 08:28 AM
#1
Thread Starter
Addicted Member
Is it a Palindrome!!!
i have a textbox and a button.I will enter some text in the textbox and on button click a msgbox will be displayed showing whether it is a palindrome or not.
I dont want to use any reverse string function......i want to check it character by character.
How to do this?
Can someone list out the steps?
Please dont provide me the code.........i need the steps only that i should perform to do the needful.........
-
Aug 17th, 2009, 08:31 AM
#2
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.
-
Aug 17th, 2009, 08:35 AM
#3
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...
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Aug 17th, 2009, 09:23 AM
#4
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.
My usual boring signature: Nothing
 
-
Aug 17th, 2009, 11:59 AM
#5
Addicted Member
Re: Is it a Palindrome!!!
 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.
-
Aug 17th, 2009, 11:59 AM
#6
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)
Last edited by stateofidleness; Aug 17th, 2009 at 12:03 PM.
-
Aug 17th, 2009, 01:08 PM
#7
Re: Is it a Palindrome!!!
@stateofidleness He wants to do it character by character and doesn't want to use any reverse functions.
-
Aug 17th, 2009, 01:17 PM
#8
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
Last edited by stateofidleness; Aug 17th, 2009 at 01:27 PM.
-
Aug 17th, 2009, 01:19 PM
#9
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
-
Aug 17th, 2009, 01:24 PM
#10
Re: Is it a Palindrome!!!
 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:
I dont want to use any reverse string function......
-
Aug 17th, 2009, 01:24 PM
#11
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
-
Aug 17th, 2009, 01:29 PM
#12
Re: Is it a Palindrome!!!
ahh very cool tech. I like that!
-
Aug 17th, 2009, 02:01 PM
#13
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
-
Aug 17th, 2009, 02:17 PM
#14
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
-
Aug 17th, 2009, 02:21 PM
#15
Re: Is it a Palindrome!!!
C'mon guys, he said no code and no reverse functions...
-
Aug 17th, 2009, 02:34 PM
#16
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|