Java Program to Check if Input String is Palindrome

In this post, we will write a Java program to check if the input String is Palindrome or not.
 
A String is said to be Palindrome if its value is the same when reversed. For example “aba” is a Palindrome String.
 
In this post, we will see three different ways we can write a program to check if the input String is Palindrome or not.

1. Using concatenation + operator

In this program, we use the concatenation + operator to reverse a string. The reverse string is checked for equality with the input original string, if both the strings are equal then the string is Palindrome.
/**
 * Java Program to Check if Input String is Palindrome
 * @author javaguides.net
 *
 */
public class StringPalindromeProgram {

 private static boolean isEmpty(final String cs) {
     return cs == null || cs.length() == 0;
 }

 public static boolean checkPalindrome(String input) {

     // Check error conditions
     if (isEmpty(input)) {
         return false;
     }
     String reverse = "";
     int length = input.length();

     for (int i = length - 1; i >= 0; i--) {
         reverse = reverse + input.charAt(i);
     }

     if (input.equals(reverse)) {
         System.out.println(input + " is palindrome = " + true);
     } else {
         System.out.println(input + " is palindrome = " + false);
     }
         return false;
 }

 public static void main(String[] args) {
     checkPalindrome("madam");
     checkPalindrome("abcba");
     checkPalindrome("abc");
  }
}
Output:
madam is palindrome = true
abcba is palindrome = true
abc is palindrome = false

2. Palindrome Program with Simple Logic

Let's compare characters in the String from both ends to find out if it’s a palindrome or not.
/**
 * Java Program to Check if Input String is Palindrome
 * @author javaguides.net
 *
 */
public class StringPalindromeProgram {

 private static void checkPalindromeString(String input) {
     boolean result = true;
     int length = input.length();
     for (int i = 0; i < length / 2; i++) {
        if (input.charAt(i) != input.charAt(length - i - 1)) {
           result = false;
           break;
         }
     }
      System.out.println(input + " is palindrome = " + result);
 }

 public static void main(String[] args) {

     checkPalindromeString("madam");
     checkPalindromeString("abcba");
     checkPalindromeString("abc");
   }
}
Output:
madam is palindrome = true
abcba is palindrome = true
abc is palindrome = false

3. Using StringBuilder reverse() Method

The String class doesn’t provide any method to reverse the String but StringBuffer and StringBuilder class has a reverse() method that we can use to check if the String is palindrome or not.
public class StringPalindromeProgram {

 private static boolean isPalindrome(String str) {
     if (str == null)
        return false;
     StringBuilder strBuilder = new StringBuilder(str);
     strBuilder.reverse();
     return strBuilder.toString().equals(str);
 }

 public static void main(String[] args) {
     System.out.println(isPalindrome("madam"));
     System.out.println(isPalindrome("abcba"));
     System.out.println(isPalindrome("abc"));
   }
}
Output:
true
true
false

Related Java String Programs with Output

Comments