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

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

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

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

24 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

Leave a Reply