java-tutorialsmade

Java program – String is Palindrome or not

In this Java Program we are going to see whether the entered string is a Palindrome or not.

Basic idea of finding a Palindrome is very simple, all you to do is reverse the string and compare both the original string and the reversed one. If both are equal then it is a palindrome.

Here is the code of the Java Class (palindrome.java):

This Java Program followed the below steps to find whether the string is Palindrome:

1. Firstly get the entered string using sc.nextLine() (where sc is the Scanner object) and store it in mystring variable.
2. Secondly remove the spaces between the string.
3. Thirdly remove any special characters to avoid confusion finding the palindrome.
4.  Now, the important step is to reverse the string and store it in a new variable revstring.
5. Finally check if both the entered string and the reversed string are same, if yes, then print “Palindrome” or else print “Not a Palindrome

Few palindrome words and sentences to check the Java program:

1. madam
2. civic
3. Madam, I’m Adam
4. A man, a plan, a canal: Panama.
5. No lemon, no melon.

If you wish you can download the script below:

download
Same palindrome script is written in PHP also, If you are a web developer you may want to have a look:
http://www.tutorialsmade.com/php-program-find-palindrome/

 

Related Posts

Leave a Reply