Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Text.RegularExpressions
- Public Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim s As String
- Dim IsItAPalindrome_1 As Boolean
- Dim IsItAPalindrome_2 As Boolean
- Dim IsItAPalindrome_3 As Boolean
- ' these function have been tested on the worlds longest Palindrome at
- ' http://norvig.com/pal17txt.html a Palindrome over 17000 words long
- ' OK lest get started... strip out all punctuation
- s = CleanInput(RichTextBox1.Text) ' you will need a RichTextBox1 to test the monster 17000 word Palindrome
- ' 3 functions
- IsItAPalindrome_1 = CheckPalindrome(s)
- IsItAPalindrome_2 = IsPalindrome(s)
- IsItAPalindrome_3 = IsPalindrome_new(s)
- End Sub
- Function CleanInput(strIn As String) As String
- ' strip out all punctuation... yep, my old favorite regular expressions again
- Return Regex.Replace(strIn.ToLower, "[^A-Za-z0-9]+", String.Empty)
- ' what we are doing there is stripping out everything except anything between the []
- End Function
- ' Credited to http://www.authorcode.com/how-to-check-whether-string-is-a-palindrome-or-not-using-vb-net/
- ' Function 1
- Private Function CheckPalindrome(ByVal strString As String) As Boolean
- Dim str As String
- str = StrReverse(strString)
- If str.Equals(strString) Then
- Return True
- Else
- Return False
- End If
- End Function
- ' Function 2
- Public Function IsPalindrome(s As String) As Boolean
- Dim i As Integer = 0
- Dim j As Integer = s.Length - 1
- While i < j
- If s(i) <> s(j) Then
- Return False
- End If
- i += 1
- j -= 1
- End While
- Return True
- End Function
- ' Function 3
- Public Function IsPalindrome_new(src As String) As Boolean
- Dim palindrome As Boolean = True
- For i As Integer = 0 To src.Length \ 2
- If src(i) <> src(src.Length - i - 1) Then
- palindrome = False
- Exit For
- End If
- Next
- Return palindrome
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement