jsp-tutorialsmade

JSP Program to find a string is Palindrome or not

Today we are going to write a JSP web application program to find the entered string is Palindrome or not.

This program is a pure JSP web application, it is not a JSP – Servlet web application. Basically the JSP code is embedded with the HTML.

Logic to find the palindrome is very simple, reverse the entered string and compare the reversed string with the original string, if both are same then it is a Palindrome.

But, only reversing the string won’t help in some cases, some time people may enter a Palindrome sentences to check, and the sentence may contain dots, commas, spaces etc., In this case there are chances to fail comparing the strings. To avoid that, we need to remove spaces, special characters and also has to change the string to lower case.

Here is the complete JSP program to find a string is Palindrome or not:

Read the comments in the program to understand it step by step.

What all you can learn from this above JSP script?

1. How to submit a Web form and get POST value in JSP? -> Ans: getParemeter().
2. How to remove spaces from string in JSP? -> Ans: replace()
3. How to use Regex to remove special characters in JSP? -> Ans: replaceAll()
4. How to Lowercase string / text in JSP? -> Ans: toLowerCase()
5. How to reverse a String in JSP? -> Ans: new StringBuilder(mystring).reverse().toString()
6. Finally, how to compare the strings in JSP? -> new String(mystring).equals(revstring)

Here are few Palindrome words / sentences to test the JSP script:

1. madam
2. mum
3. civic
4. Madam, I’m Adam
5. No lemon, no melon.

 

Related Posts

Leave a Reply