A palindrome is a word, phrase, number, or other sequence of units that may be read the same way in either direction, generally if used comma, separators or other word dividers are ignored. [Not mandatory]
Similarly, palindrome numbers are those numbers which represent same number if all digits are reversed (Underscores can be ignored in large numbers such as 1_00_00_001). Underscores in numeric literals are new addition in Java 7 features.
1. Java palindrome string example
To check palindrome string, reverse the String characters. Now use String.equals() method to verify if given string was palindrome or not.
1.1. Check string palindrome using Apache commons StringUtils
Simple palindrome program in Java for string. It is also palindrome program in java using reverse method.
public class Main { public static void main(String[] args) { System.out.println( isPalindromeString("howtodoinjava") ); //false System.out.println( isPalindromeString("abcba") ); //true } public static boolean isPalindromeString(String originalString) { String reverse = StringUtils.reverse(originalString); return originalString.equals(reverse); } }
1.2. Check string palindrome using StringBuilder
This same logic can be applied with StringBuffer class as well.
public class Main { public static void main(String[] args) { System.out.println( isPalindromeString("howtodoinjava") ); System.out.println( isPalindromeString("abcba") ); } public static boolean isPalindromeString(String originalString) { String reverse = new StringBuilder(originalString).reverse().toString(); return originalString.equals(reverse); } }
1.3. Check string palindrome with for loop
Use for loop to get the reverse string by iterating over string characters from last index using charAt() method and create new string.
Use this approach only when you are checking string palindrome in java without using reverse method.
public class Main { public static void main(String[] args) { System.out.println( isPalindromeString("howtodoinjava") ); System.out.println( isPalindromeString("abcba") ); } public static boolean isPalindromeString(String originalString) { String reverse = ""; int length = originalString.length(); for ( int i = length - 1; i >= 0; i-- ) reverse = reverse + originalString.charAt(i); return originalString.equals(reverse); } }
2. Java palindrome number example
To verify, if a given number is palindrome number is or not, we need to reverse the digits in number and compare with original number if both are equal or not.
package com.howtodoinjava.puzzle; public class PalindromeTest { /** * Test the actual code if it works correctly * */ public static void main(String[] args) { System.out.println(checkIntegerPalindrome( 100 )); //false System.out.println(checkIntegerPalindrome( 101 )); //true System.out.println(checkIntegerPalindrome( 500045 )); //false System.out.println(checkIntegerPalindrome( 50005 )); //true } /** * This function will test the equality if a number and its reverse. * @return true if number is palindrome else false * */ public static boolean checkIntegerPalindrome(int number) { boolean isPalindrome = false; if(number == reverse(number)) { isPalindrome = true; } return isPalindrome; } /** * This function will reverse a given number. * @return reverse number * */ public static int reverse(int number) { int reverse = 0; int remainder = 0; do { remainder = number % 10; reverse = reverse * 10 + remainder; number = number / 10; } while (number > 0); return reverse; } } Program Output. false true false true
Happy Learning !!
Leave a Reply