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
OUTPUT of the above code
Explanation of the code
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.
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
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
}
}
ABCDEFFEDCBA IS A palindrome ABCDEFFEDCBB IS NOT A palindrome 2345665432 IS A palindrome 234566544 IS NOT A palindrome
isPalindrome(String)
method checks if the String passed to it is a palindrome by executing 3 steps -- The input
String
, namedinputStr
, is reversed by reading it character-by-character from the right using theString.charAt()
method. The characters extracted in reverse are stored are appended to aStringBuffer
instance namedreverseStr
. reverseStr
is then converted to aString
usingtoString()
method and then compared withinputStr
.We ignore the case of the two Strings by usingString.equalsIgnoreCase()
method for the comparison.- If
inputStr
equalsreverseStr
then we returntrue
else we returnfalse
.
- The input
- Similar to the previous method, overloaded
isPalindrome(long)
method checks if thelong
value passed to it is a palindrome by executing 3 steps -- We progressively read the digits of the input number, named
inputNo
, in reverse. This is accomplished by usingmod(%) 10
which gives us the last digit, which in turn is added to the currently constructed reversed number variable, namedreverseNo
, multiplied by 10 (multiplication by 10 progressively increments the place value of all digits ofreverseNo
). reverseNo
is compared to theinputNo
.- If
reverseNo
equalsinputNo
thentrue
is returned, elsefalse
is returned.
- We progressively read the digits of the input number, named
main()
method invokesisPalindrome(String)
andisPalindrome(long)
with 2 inputs each. It prints for each inputString
/long
value whether itIS A palindrome
orIS NOT A palindrome
which is the output.