Advertisement
NAK

Is Palindrome

NAK
Apr 21st, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.23 KB | None | 0 0
  1. Imports System.Text.RegularExpressions
  2.  
  3. Public Class Form1
  4.  
  5.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  6.         Dim s As String
  7.         Dim IsItAPalindrome_1 As Boolean
  8.         Dim IsItAPalindrome_2 As Boolean
  9.         Dim IsItAPalindrome_3 As Boolean
  10.  
  11.         ' these function have been tested on the worlds longest Palindrome at
  12.         ' http://norvig.com/pal17txt.html a Palindrome over 17000 words long
  13.  
  14.         ' OK lest get started... strip out all punctuation
  15.         s = CleanInput(RichTextBox1.Text) ' you will need a RichTextBox1 to test the monster 17000 word Palindrome
  16.  
  17.         ' 3 functions
  18.         IsItAPalindrome_1 = CheckPalindrome(s)
  19.         IsItAPalindrome_2 = IsPalindrome(s)
  20.         IsItAPalindrome_3 = IsPalindrome_new(s)
  21.  
  22.     End Sub
  23.     Function CleanInput(strIn As String) As String
  24.         ' strip out all punctuation... yep, my old favorite regular expressions again
  25.         Return Regex.Replace(strIn.ToLower, "[^A-Za-z0-9]+", String.Empty)
  26.     ' what we are doing there is stripping out everything except anything between the []
  27.     End Function
  28.  
  29.     ' Credited to http://www.authorcode.com/how-to-check-whether-string-is-a-palindrome-or-not-using-vb-net/
  30.     ' Function 1
  31.     Private Function CheckPalindrome(ByVal strString As String) As Boolean
  32.         Dim str As String
  33.         str = StrReverse(strString)
  34.         If str.Equals(strString) Then
  35.             Return True
  36.         Else
  37.             Return False
  38.         End If
  39.     End Function
  40.  
  41.     ' Function 2
  42.     Public Function IsPalindrome(s As String) As Boolean
  43.         Dim i As Integer = 0
  44.         Dim j As Integer = s.Length - 1
  45.         While i < j
  46.             If s(i) <> s(j) Then
  47.                 Return False
  48.             End If
  49.             i += 1
  50.             j -= 1
  51.         End While
  52.         Return True
  53.     End Function
  54.  
  55.     ' Function 3
  56.     Public Function IsPalindrome_new(src As String) As Boolean
  57.         Dim palindrome As Boolean = True
  58.         For i As Integer = 0 To src.Length \ 2
  59.             If src(i) <> src(src.Length - i - 1) Then
  60.                 palindrome = False
  61.                 Exit For
  62.             End If
  63.         Next
  64.         Return palindrome
  65.     End Function
  66.  
  67. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement