Palindrome word can be defined as string that have same meaning when read from Left-Right or Right-Left.Below function can check if given word is Palindrome or not.
In below code one can also learn how to use Recursive function.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strInput as string = "racecar"
If IsPalinDrome(0, strInput.Length - 1, strInput) Then Messagebox.show(strInput & " is Palindrome") Else Messagebox.Show(strInput & " is Non Palindrome") End if
End Sub
'Below is Recursive function that varifies if word is Palindrome or not Private Function IsPalinDrome(ByVal intStart As Integer, ByVal intEnd As Integer, ByVal strWord As String) As Boolean Try 'This is Termination condition for Palindrome string If intStart > intEnd Then Return True End If 'This is Termination condition for Non-Palindrome string 'If Mirror character do not match breack the Recursive function If strWord.Chars(intStart) <> strWord.Chars(intEnd) Then Return False End If 'If Mirror Character matches,continue with Recursive function If strWord.Chars(intStart) = strWord.Chars(intEnd) Then Return IsPalinDrome(intStart + 1, intEnd - 1, strWord) End If
Catch exc As Exception Throw exc End Try
End Function
|
No responses found. Be the first to respond and make money from revenue sharing program.
|