Java Programming Example to check whether the given string is palindrome or not?

This program will accept string from the user through keyboard and display whether the given string is palindrome or not.

import java.io.*;

/**
 *
 * @author JavaHotSpot
 */

public class palindrome
{
  public static void main(String args[])
 {
     String s1,s2;
     int length;
     DataInputStream d;
     try
     {
     System.out.println("Enter the String");
     d=new DataInputStream(System.in);
     s1=(String)d.readLine();
     StringBuffer sb;
     sb=new StringBuffer(s1);
     s2=sb.reverse().toString();
     System.out.println("The Reverse of the string is "+s2);
     if(s1.equals(s2))
     {
         System.out.println("The given string is palindrome");
     }
     else
     {
         System.out.println("The given string is not palindrome");
     }
     }
     catch(Exception e)
     {
         System.out.println(e.toString());
     }
 }
}

Output:

Run 1:
Enter the String
Javaatweet
The Reverse of the string is teewaavaJ
The given string is not palindrome

Run 2:
Enter the String
madam
The Reverse of the string is madam
The given string is palindrome