How to find whether the given string is palindrome or not in java


import java.io.*;
class Mainstring
 {
  public static void main(String args[]) throws IOException
   {
    String str;
    DataInputStream din=new DataInputStream(System.in);
    System.out.println("enter string");
    str=din.readLine();
    Palindrome p=new Palindrome(); //Creating object to the class Palindrome
    if(p.checkpalindrome(str))          
     { System.out.println("entered string is a palindrome");
     }
    else
     { System.out.println("entered string is not a palindrome");
     }
   }
}
class Palindrome
 {
  boolean checkpalindrome(String str)   //Method to check the given string
   {
    StringBuffer sb=new StringBuffer(str);
    StringBuffer rev=sb.reverse();
    String revstring=new String(rev);
    return(str.equals(revstring));
  }
}

OUTPUT of above program is

enter string
karthik
entered string is not a palindrome

Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment