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 it’s value is 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.

Palindrome Program with Reversing a String

In this program, we use concatenation + operator to reverse a string. The reverse string is check for equality with 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

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

Palindrome Program with Reversing a String using StringBuilder reverse() Method

String class doesn’t provide any method to reverse the String but StringBuffer and StringBuilder class has reverse method that we can use to check if 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
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Comments