JB Header
Java program to check if number or string is a palindrome
Introduction This tutorial first defines a palindrome. It then provides a short algorithm to determine whether a sequence of characters or numbers is a palindrome. Lastly, it provides the Java code for determining whether the given number or string is a palindrome or not, without using inbuilt String functions in Java, along with explanation of the code. What is a Palindrome A palindrome is a word, phrase, number or a sequence of characters which reads the same when read from either directions. I.e. you will encounter the same sequence of numbers or characters when moving from left to right or right to left with a palindrome. Examples of palindromes - Noon, radar, madam, redder. Algorithmic steps to determine whether the given number or a string is a palindrome Palindrome determination algorithm consists of 3 simple steps -
STEP 1: Reverse the given number or string.
STEP 2: Compare the original number or string with the reversed one.
STEP 3: If the original and reversed match then it is a palindrome, else it is not a palindrome.
Java code to check if the given number or string is a palindrome
Java code to check if the given number or string is a palindrome
package com.javabrahman.generaljava;
public class PalindromeNumberOrString {
  public static void main(String args[]){
    //Checking 2 input Strings-inputStr1 & inputStr2
    String inputStr1="ABCDEFFEDCBA";
    String inputStr2="ABCDEFFEDCBB";
    System.out.println(inputStr1+" IS "+((isPalindrome(inputStr1))? "A":"NOT A")+" palindrome");
    System.out.println(inputStr2+" IS "+((isPalindrome(inputStr2))? "A":"NOT A")+" palindrome");

    //Checking 2 input numbers-inputStr1 & inputStr2
    long inputNo1=2345665432l;
    long inputNo2=234566544l;
    System.out.println(inputNo1+" IS "+((isPalindrome(inputNo1))? "A":"NOT A")+" palindrome");
    System.out.println(inputNo2+" IS "+((isPalindrome(inputNo2))? "A":"NOT A")+" palindrome");
  }
  
  //method to check whether input String is a palindrome
  public static boolean isPalindrome(String inputStr){
    StringBuffer reverseStr=new StringBuffer("");
    for(int i=inputStr.length()-1;i>=0;i--){
      reverseStr.append(inputStr.charAt(i));
    }
    if(inputStr.equalsIgnoreCase(reverseStr.toString())){
      return true;//inputStr is a palindrome
    }
    return false;//inputStr is not a palindrome
  }
  
  //method to check whether input number is a palindrome
  public static boolean isPalindrome(long inputNo){
    long reverseNo=0;
    long num=inputNo;
    while(num > 0){
      long remainder=num % 10;
      reverseNo=reverseNo*10+remainder;
      num = num/10;
    }
    if(inputNo == reverseNo){
      return true;//inputNo is a palindrome
    }
    return false;//inputNo is not a palindrome
  }
}
 OUTPUT of the above code
ABCDEFFEDCBA IS A palindrome
ABCDEFFEDCBB IS NOT A palindrome
2345665432 IS A palindrome
234566544 IS NOT A palindrome
Explanation of the code
  • isPalindrome(String) method checks if the String passed to it is a palindrome by executing 3 steps -
    1. The input String, named inputStr, is reversed by reading it character-by-character from the right using the String.charAt() method. The characters extracted in reverse are stored are appended to a StringBuffer instance named reverseStr.
    2. reverseStr is then converted to a String using toString() method and then compared with inputStr.We ignore the case of the two Strings by using String.equalsIgnoreCase() method for the comparison.
    3. If inputStr equals reverseStr then we return true else we return false.
  • Similar to the previous method, overloaded isPalindrome(long) method checks if the long value passed to it is a palindrome by executing 3 steps -
    1. We progressively read the digits of the input number, named inputNo, in reverse. This is accomplished by using mod(%) 10 which gives us the last digit, which in turn is added to the currently constructed reversed number variable, named reverseNo, multiplied by 10 (multiplication by 10 progressively increments the place value of all digits of reverseNo).
    2. reverseNo is compared to the inputNo.
    3. If reverseNo equals inputNo then true is returned, else false is returned.
  • main() method invokes isPalindrome(String) and isPalindrome(long) with 2 inputs each. It prints for each input String/long value whether it IS A palindrome or IS NOT A palindrome which is the output.
Conclusion In this tutorial we looked palindrome definition, algorithm for palidrome identification/determination and an example program in Java showing how to determine whether a given number or string is a palindrome or not.