Write a Java program that checks whether a given string is a palindrome or not. Ex MADAM is a palindrome.
Program Code:
import java.io.*;
class Palindrome
{
public static void main(String[] args) throws Exception
{
String str1, str2;
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter a String");
str1=dis.readLine();
StringBuffer tmp=new StringBuffer(str1);
tmp.reverse();
str2=new String(tmp);
System.out.println("The String after Reverse is "+str2);
if(str1.equals(str2))
System.out.println("The given String "+str1+" is palindrome");
else
System.out.println("The given String "+str1+" is not palindrome");
}
}
Post A Comment:
0 comments: