Ask Experts Questions for FREE Help !
Ask
    peter1988's Avatar
    peter1988 Posts: 12, Reputation: 1
    New Member
     
    #1

    Sep 29, 2009, 06:57 AM
    Palindrome in Visual Basic 2008
    May anyone tell me how to create a palindrome function in Visual Basic 2008, I need some help in my project...
    Perito's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #2

    Sep 29, 2009, 07:27 AM

    If a palindrome is the same read forward and backward, then this is simple.

    Simply create a string of random characters using only numerals. Once the string is half as long as you want the final string to be, add the characters to the end of the string, but in reverse order. You also can, optionally, skip the last character of the original string as it can stand alone. Here's one way to do it, but certainly not the only way.

    Private Function GeneratedPalindromeValue(ByVal DesiredLength As Integer) As Integer
    Dim tStr As String = ""
    Dim ProcessingLength As Integer = DesiredLength \ 2 ' Note integer division
    Dim tChar As String
    Randomize() ' so we don't get the same string every time we run this.

    For i As Integer = 0 To ProcessingLength
    tChar = Rnd().ToString ' generate a "Single" and convert it to a string.
    tStr += tChar.Substring(tChar.Length - 1, 1) ' grab the last numeral in the string
    Next

    Dim J As Integer = tStr.Length - 1 ' index to point to characters in tStr
    If (DesiredLength \ 2) * 2 <> DesiredLength Then ' odd length?
    J -= 1 ' odd length, don't duplicate the last character
    ProcessingLength -= 1 ' so we don't duplicate the middle character
    End If
    For i As Integer = 0 To ProcessingLength
    tStr += tStr.Substring(J, 1)
    J -= 1 ' decrement j, the index.
    Next

    Return Integer.Parse(tStr)
    End Function

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Something about Visual Basic. [ 3 Answers ]

May anyone tell me how to do this calculation,the code into Visual Basic? (Question) If the user inputs 15 and 3, the first number is a multiple of the second. If the user inputs 2 and 4, the first number is not a multiple of the second.

Visual basic [ 1 Answers ]

I already finish a very simple calculator,actually it has only four buttons. at first its working fine but when add another feature its not working anymore. What I want to do is when press 1 then press + then press 1 and press + again it should add the first two number get ready to add another...

Visual basic [ 1 Answers ]

I am doing visual basic program. Could you help with the code how to login.

Visual Basic 6.0 [ 1 Answers ]

What is the cost of Visual Studio 6.0 Enterprise Edition?


View more questions Search