Check Whether Given String is Palindrome or Not in VB
The palindrome strings or numbers are always fun because we can read those number or string from both left and right side and both are equal. There are many words in English like level, noon, radar, etc and numbers like 12321, 45654, etc. My mother tongue is ‘Malayalam’, which is also a palindrome.
Check whether the given string is palindrome or not is a common question in programming language. Solving this question in C, Java is not easy, but which is easy in modern programming languages like C#, VB etc. Here some string inbuilt functions make easy to solve the problem.
Let us see, how to make a Visual Basic program to check whether a string is palindrome or not.
Here a textbox (textbox1) accept a string and a button (button1) event checks the given string is palindrome and not.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = StrReverse(TextBox1.Text) Then
MessageBox.Show(“Not a Palindrome”)
How the code works?
I wrote above code under Button1 click event. TextBox1.Text is the text that we enter in the textbox 1. StrReverse is an inbuilt string function which returns the reverse of the given parameter string. So StrReverse(TextBox1.Text) gets the reverse string of the given string that we entered in the textbox1.
If condition compare given string and reverse of the string. If both are equal, then the string should be a palindrome and message box shows ‘palindrome’. If the condition is false, then message box shows ‘Not a palindrome’.
If we want to write same program without using inbuilt string function, then we have to read every letters from left side and right site then compare. Here StrReverse function contains same code with this logic. But we don’t mind the inner codes. That is the benefit of inbuilt functions.
Sample Screen short: