Write a Java program that checks whether a given string is a palindrome or not. Ex MADAM is a palindrome.

Program Code in JAVA:
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");
}
}


Program Output:
c:\java>javac Palindrome.java
c:\java>java Palindrome
Enter a String
madam
The String after Reverse is madam
The given String madam is palindrome
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: