Java program that checks whether the given string is palindrome or not

class palindrome
{
public static void main(String[] args)
{
StringBuffer s1=new StringBuffer(args[0]);
StringBuffer s2=new StringBuffer(s1);
s1.reverse();
System.out.println(“Given String is:”+s2);
System.out.println(“Reverse String is”+s1);
if(String.valueOf(s1).compareTo(String.valueOf(s2))==0)
System.out.println(“Palindrome”);
else
System.out.println(“Not Palindrome”);
}
}

Output:

Java palindrome madam
Given String is:madam
Reverse String is madam
Palindrome

Java palindrome harish
Given String is:harish
Reverse String is hsirah
Not Palindrome

29 Responses to “Java program that checks whether the given string is palindrome or not”

  1. Another Simple one without importing any API’s,

    public class FindPalindromeOrNot {

    public static void main(String[] ar) {

    String inputString = “liril”;

    boolean isPalindrome = true;
    char[] inputStringArr = inputString.toCharArray();
    int length = inputStringArr.length;
    int temp = length;
    for (int n = 0; n < length; n++) {
    temp -= 1;
    if (inputStringArr[n] != inputStringArr[temp]) {
    isPalindrome = false;
    }

    }

    System.out.println("Is Palindrome = " + isPalindrome);

    }
    }

    Reply
  2. how do I make a program to show if word, sentence or number is palindrome

    Reply
  3. sundaresan

    when i execute the above program, it shows java.lang.ArrayIndexOutOfBoundaryException.
    can anyone solve this ?

    Reply
  4. i want a programme for palindrome word ex…
    input nitin is sweet boy
    output nitin sweet

    Reply

Leave a Reply