Sunday, 7 December 2014

WAP to test whether a string is palindrome or not

//Palindrome.java
import java.io.*;

class Palindrome
{
public static void main(String[] args) throws IOException
{
//accept the string from keyboard
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string : ");
String str=br.readLine();

//store a copy of original string in temp
        String temp =str;

//convert the string in to stringBuffer
StringBuffer sb = new StringBuffer(str);

//now reverse the string in stringBuffer
// sb.reverse();
        System.out.println(sb.reverse()+" after reverse");

//convert the stringbuffer in string
  str= sb.toString();

//compare the original string  available in temp with this reversed string

if(temp.equalsIgnoreCase(str))
 System.out.println(temp+" is palindrome");
else
   System.out.println(temp+" is not palindrome");
}
}

No comments:

Post a Comment