Results 1 to 16 of 16

Thread: Is it a Palindrome!!!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    135

    Unhappy 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.........

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  5. #5
    Addicted Member
    Join Date
    Jun 2009
    Posts
    245

    Re: Is it a Palindrome!!!

    Quote Originally Posted by mickey_pt View Post
    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.

  6. #6
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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)

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Is it a Palindrome!!!

    @stateofidleness He wants to do it character by character and doesn't want to use any reverse functions.

  8. #8
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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:
    1. Private Sub checkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkButton.Click
    2.         Dim BeginString() As Char = myStringTextbox.Text.ToCharArray
    3.         Dim ReverseString() As Char = myStringTextbox.Text.ToCharArray
    4.         Array.Reverse(ReverseString)
    5.         Dim i As Integer
    6.         For i = 0 To BeginString.Length - 1
    7.             If BeginString(i).ToString = ReverseString(i).ToString Then
    8.                 MsgBox("so far so good")
    9.             Else
    10.                 MsgBox("definitely -NOT- a palindrome")
    11.                 Exit Sub
    12.             End If
    13.         Next
    14.     End Sub

    he'd still be going char by char, he'd just have two strings instead of 1

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Is it a Palindrome!!!

    Quote Originally Posted by stateofidleness View Post
    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:
    1. Dim BeginString() As Char = yourString.ToCharArray
    2.  
    3. Dim ReverseString() As Char = yourString.ToCharArray
    4. Array.Reverse(ReverseString)
    5.  
    6. Dim i as integer
    7. for i = 0 to BeginString.Length
    8.      'compare and do work
    9. 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......

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

  13. #13
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. Private Function IsPalindrome(ByVal input As String)
    2.     Return input.StartsWith(Strings.StrReverse(input.Substring(input.Length \ 2)))
    3. End Function
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  14. #14
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Is it a Palindrome!!!

    Here is the loop version that doesn't use any string reverse function as you need:

    vb Code:
    1. Private Function IsPalindrome(ByVal input As String)
    2.     For i As Integer = 0 To input.Length \ 2
    3.         If 0 <> String.Compare(input.Chars(i), input.Chars(input.Length - 1 - i), True) Then Return False
    4.     Next
    5.     Return True
    6. End Function
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  15. #15
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Is it a Palindrome!!!

    C'mon guys, he said no code and no reverse functions...

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width