Example Java String palindrome checker program using recursion

How to write a Java palindrome program for Strings

Good programmers need to create code that efficiently solves problems, using various methods. A popular academic exercise is to create a program that determines if a number or String is a palindrome.

To write a Java String palindrome program, follow these steps:

  1. Obtain the text String to check through a literal declaration or user input.
  2. Declare a boolean property to indicate if the Java String is a palindrome.
  3. Evaluate corner cases such as the String being null, a single character or zero characters.
  4. Compare the first character to the last character in the string. If they are not equal, return false.
  5. Move forward and backwards one character at a time and repeat this comparison.
  6. If the loop never generates a false value, then the Java String is a palindrome.
  7. If the loop generates a false value, the Java String is not a palindrome.

Recursive Java palindrome program example

Here is an example of a recursive Java palindrome program that checks String literals:

package com.mcnz.palindrome.example;

public class JavaStringPalindromeProgram {
  /* The main method declares three Strings for 
     the Java palindrome program to check. */
  public static void main(String[] args) {
    boolean flag = javaPalindromeCheck("sitonapanotis");
    System.out.println(flag);
    flag = javaPalindromeCheck("nine");
    System.out.println(flag);
    flag = javaPalindromeCheck("amanaplanacanalpanama");
    System.out.println(flag);

  }
  /* This method does a recursive Java palindrome check */
  public static boolean javaPalindromeCheck(String s){
    if(s.length() == 0 || s.length() == 1) {
      return true;
    }
    if(s.charAt(0) == s.charAt(s.length()-1)) {
      return palindromeCheck(s.substring(1, s.length()-1));
    }
    return false;
  }  /* End of Java String palindrome checker method */
} /* End of Java palindrome program */

Example Java palindrome checker results

When this program runs, it returns:

  • true for sitonapanotis
  • false for nine
  • true for amanaplanacanalpaname

Other Java Strings to palindrome check

Here are 10 other text Strings for which the Java palindrome checker program should return a true value. Put these into your code and test them:

  • Do geese see God?
  • radar
  • No lemon, no melon
  • peep
  • Hannah
  • Able was I, ere I saw Elba.
  • Taco cat
  • Never odd or even
  • Madam, I’m Adam.
  • Red rum, sir, is murder

With these examples, your Java palindrome program must decide whether to ignore non-text characters and punctuation, which will add to the complexity of the program.

Java palindrome with loops and arrays

This Java palindrome example uses recursion, which is an advanced concept.

There are also drawbacks to recursion, as it can place a heavy load on the Java stack and eventually trigger a StackOverflowError and terminate your program

Other approaches to solve the Java String palindrome problem include the use of loops and arrays.

Test your skills and see if you can implement your own Java palindrome program with loops, arrays and maybe even the Scanner class to dynamically generate input from the user.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close