Palindrome checker in Java

Upasana | July 24, 2020 | 2 min read | 1,394 views | Java Coding Challenges


Palindrome

A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar or the number 10801.

Palindrome checker in Java

In simple words, A Palindrome Number is a number that even when reversed is same as original number.

Pseudo code

So our approach will be quite simple:

  1. Get the input number and copy it in temporary variable

  2. Reverse the original number

  3. Compare if the temporary number is same as the reversed number

  4. If both are same then input is a palindrome else not

Let’s write the implementation in java.

Palindrome Checker
public class Palindrome {

  boolean isPalindrome(int input) {
      int reversed = reverse(input);
      return input == reversed;
  }

  private int reverse(int input) {
      int lastDigit, sum = 0, temp;
      temp = input;
      while (temp > 0) {
          lastDigit = temp % 10; (1)
          sum = (sum * 10) + lastDigit;
          temp = temp / 10;
      }
      return sum; (2)
  }
}
1 We are using remainder operator to get the last digit of number
2 sum is the reversed number

JUnit testcase

We can write simple JUNIT testcase to assert that basic functionality of palindrome checker program.

Few testcases, that we can think of:

  1. check a palindrome number

  2. check a non-palindrome number

  3. check zero as the input

  4. check for negative numbers

JUnit Testcase
import org.junit.jupiter.api.Test

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.assertThrows

class PalindromeNumberCheckerTest {

    @Test
    fun `check a palindrome number`() {
        val checker = PalindromeNumberChecker()
        assertEquals(true, checker.isPalindrome(1001))
    }

    @Test
    fun `check a non-palindrome number`() {
        val checker = PalindromeNumberChecker()
        assertEquals(false, checker.isPalindrome(12))
    }

    @Test
    fun `check if zero is palindrome`() {
        val checker = PalindromeNumberChecker()
        assertEquals(true, checker.isPalindrome(0))
    }

    @Test
    fun `check negative numbers`() {
        val checker = PalindromeNumberChecker()
        val exception = assertThrows<AssertionError> {
            checker.isPalindrome(-100)
        }
        assertEquals("input must be positive number", exception.message)
    }
}

That’s all!

Here we are only considering integers for Palindrome, but the same logic can be applied to any other input type (word, phrase, etc)
Did you know?
The longest palindromic word in the Oxford English Dictionary is the onomatopoeic tattarrattat, coined by James Joyce in Ulysses (1922) for a knock on the door

If you are looking for more complex example, where we do the bit wise reversal of a number to check if it is palindrome or not, follow this article:

For String input, you can refer to Check if a given String is palindrome

You can download sample project from Github

https://github.com/cancerian0684/junit5-gradle-kotlin-sample


Java Coding Challenges:
  1. Reverse position of words in a string using recursion
  2. Check if the given string is palindrome
  3. Find two numbers of which the product is maximum in an array
  4. Prime number checker in Java
  5. Create anagram buckets from a given input array of words
  6. Anagrams string checker in Java
  7. Reverse a string using recursion in Java
See all articles in Java Coding Challenges
Top articles in this category:
  1. Check if the given string is palindrome
  2. 50 Java Interview Questions for SDET Automation Engineer
  3. Java Coding Problems for SDET Automation Engineer
  4. Anagrams string checker in Java
  5. Junit interview questions for SDET automation engineer
  6. Rest Assured API Testing Interview Questions
  7. Prime number checker in Java

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.