
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Palindrome in Java
What is a Palindrome?
A palindrome is a number, string or any data value, which reads the same from backwards. i.e. If we reverse a value and compare it with its original value, the result should be true.
For example, strings like "ABA", "RADAR", "BABAB" and numbers like 121, 12321 are palindromes.
Checking for Palindrome Using Java Program
One way to verify if a string, number or, an alphanumeric value is a palindrome is by reversing the given it. Following are the steps -
- Get the value to be verified.
- Reverse the contents of the given value.
- Compare the original value to the reversed value.
- If the result is "true", given number is a palindrome.
Instead of reversing the entire string we can also compare characters from beginning of the string with the characters from end of the string.
Using a While Loop and "%" Operator
If the given value is a number we can reverse it, using the while loop and the "%" operator. If we perform the modulo operation on a number and 10 the result would be its digit at the units place (from last).
- Get the digit from last using modulo operator and store it in a separate variable (say dig).
- Initialize another variable with 0 (say reversedNum). Add the dig value to the reversedNumafter multiplying it by 10.
- Divide the number by 10.
- If you repeat this till the number becomes 0 (while). The reversedNum holds the reversed value of the original number.
- Compare the original and reversed numbers using the "==" operator and print the result.
Example
In this example, we will take a number, reverse it with the help of a while loop and check if the reversed number is equal to the original number.
public class Main{ public static void main(String[] args){ int num = 121; int reversedNum = 0; int actualNum = num; while(num != 0){ int dig = num % 10; reversedNum = reversedNum * 10 + dig; num = num / 10; } if(actualNum == reversedNum){ System.out.println(actualNum + " is a palindrome number."); }else{ System.out.println(actualNum + " is not a palindrome number."); } } }
Output
121 is a palindrome number.
Using a the reverse() Method
If the given value is a string we can use the reverse() method of the StringBuilder and StringBuffer classes of the java.util package.
This method reverses the contents of the current object (StingBuilder of StringBuffer) and returns it (as an object of the same class). We can convert the resultant value to a string using the toString() method.
To compare both String values we can use the equals() method of the String class.
Example 1
Now, let's check if a string is palindrome or not. We will use the StringBuilder class to reverse the string and check if the reversed string is equal to the original string.
public class PalindromeString{ public static void main(String[] args){ String str = "ABA"; StringBuilder sb = new StringBuilder(str); sb.reverse(); if(str.equals(sb.toString())){ System.out.println(str + " is a palindrome string."); }else{ System.out.println(str + " is not a palindrome string."); } } }
Output
ABA is a palindrome string.
Example 2
We will use the built-in method named reverse() of the StringBuffer class to reverse the given string. Then, we pass this reversed string to the equals() method to check if both original and reversed strings are the same or not. If both are the same then, string is a palindrome; otherwise not.
public class PalindromeString{ public static void main(String[] args){ String str = "ABA"; StringBuffer sb = new StringBuffer(str); sb.reverse(); if(str.equals(sb.toString())){ System.out.println(str + " is a palindrome string."); }else{ System.out.println(str + " is not a palindrome string."); } } }
Output
ABA is a palindrome string.
Using the charAt() Method
The String class in Java provides a method named charAt(). This method accepts an integer value representing an index as parameter, and reruns the character at the given index.
Using this method we can retrieve the characters from characters from beginning of the string with the respective characters from end of the string. (charAt(length) and charAt(length-1)).
In other word, we need to check if the first character is equal to the last character, second character is equal to second last character and so on.
If any one character is not equal to its respective character from last the given string is not a palindrome.
Example
In this example, we will take a string and check if it is palindrome or not using the charAt() method.
public class PalindromeString{ public static void main(String[] args){ String str = "ABA"; boolean isPalindrome = true; int len = str.length(); for(int i=0; i < len/2; i++){ if(str.charAt(i) != str.charAt(len-i-1)){ isPalindrome = false; break; } } if(isPalindrome){ System.out.println(str + " is a palindrome string."); }else{ System.out.println(str + " is not a palindrome string."); } } }
Output
ABA is a palindrome string.