Ask Experts Questions for FREE Help!
Answer   ||    Advanced Search

Ask your question or search...
International Sites: Nederlandse experts vragen
User Name 
Password 
Join   Forgot password? 

Home > Computers & Technology > Programming > Compiled Languages > Visual Basic   »   Palindrome in Visual Basic 2008

Question
 
 
#1  
Old Sep 29, 2009, 06:57 AM
peter1988
New Member
peter1988 is offline
 
Join Date: Oct 2008
Posts: 5
peter1988 See this member's comment history on his/her Profile page.
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...

Reply With Quote
 
     

Answers
 
 
Old Sep 29, 2009, 07:27 AM   #2  
Ultra Member
Perito is online now
 
Perito's Avatar
 
Join Date: Feb 2009
Location: USA
Posts: 2,889
Perito See this member's comment history on his/her Profile page.Perito See this member's comment history on his/her Profile page.
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
  Reply With Quote
 
     

Your Answer
Email me when someone replies to my answer
Join Login



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Ask your question or search...



Similar Threads
Something about Visual Basic.
(1 replies)
visual basic
(1 replies)
Visual Basic
(0 replies)
Visual basic
(1 replies)
Visual Basic 6.0
(1 replies)

Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page
Search this Thread

Advanced Search

Bookmarks





Copyright ©2003 - 2009, Ask Me Help Desk.
All times are GMT -8. The time now is 08:02 AM.