ZetCode

Java Palindrome

last modified July 6, 2020

Java Palindrome tutorial shows how to work with palindromes in Java.

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

In this tutorial we show several ways to check if a string is a palidrome in Java.

Java Palindrome with StringBuilder

The StringBuilder's reverse method causes this character sequence to be replaced by the reverse of the sequence.

com/zetcode/Palindrome.java
package com.zetcode;

public class Palindrome {

    public static void main(String[] args) {

        System.out.println(isPalindrome("radar"));
        System.out.println(isPalindrome("kayak"));
        System.out.println(isPalindrome("forest"));
    }

    private static boolean isPalindrome(String original) {

        String reversed = new StringBuilder(original).reverse().toString();
        return original.equals(reversed);
    }
}

In the example, we pass the original string to the StringBuilder and reverse it. Then we compare the reversed string to the original with equals.

Java Palindrome with for loop and charAt

The String's charAt method returns the char value at the specified index. An index ranges from 0 to length() - 1.

com/zetcode/Palindrome2.java
package com.zetcode;

public class Palindrome2 {

    public static void main(String[] args) {

        System.out.println(isPalindrome("radar"));
        System.out.println(isPalindrome("kayak"));
        System.out.println(isPalindrome("forest"));
    }

    private static boolean isPalindrome(String original) {

        String reversed = "";

        int len = original.length();

        for (int i = len - 1; i >= 0; i--) {
            
            reversed = reversed + original.charAt(i);
        }

        return original.equals(reversed);
    }
}

In the example, we build the reversed string with a for loop that goes from the end of the original string. We get the current character with charAt.

int len = original.length();

We get the number of characters in the original string with length.

for (int i = len - 1; i >= 0; i--) {
        
    reversed = reversed + original.charAt(i);
}

We build the reversed string by taking the characters from the end of the original string with charAt.

Java Palindrome with toCharArray

The String's toCharArray converts the string to a new character array.

com/zetcode/Palindrome3.java
package com.zetcode;

public class Palindrome3 {

    public static void main(String[] args) {

        System.out.println(isPalindrome("radar"));
        System.out.println(isPalindrome("kayak"));
        System.out.println(isPalindrome("forest"));
    }

    private static boolean isPalindrome(String original) {

        char[] data = original.toCharArray();

        int i = 0;
        int j = data.length - 1;

        while (j > i) {

            if (data[i] != data[j]) {
                return false;
            }

            ++i;
            --j;
        }
        
        return true;
    }
}

In the example, we turn the original string to an array of characters. In a while loop, we start to compare the characters from the both sides of the string; starting with the leftmost and rightmost characters. We go until the middle of the string.

Java Palindrome with Stack

The Stack is a last-in-first-out (LIFO) collection.

com/zetcode/Palindrome4.java
package com.zetcode;

import java.util.Stack;

public class Palindrome4 {

    public static void main(String[] args) {

        System.out.println(isPalindrome("radar"));
        System.out.println(isPalindrome("kayak"));
        System.out.println(isPalindrome("forest"));
    }

    private static boolean isPalindrome(String original) {

        char[] data = original.toCharArray();

        Stack<Character> stack = new Stack<>();

        for (char c: data) {

            stack.push(c);
        }

        char[] data2 = new char[data.length];

        int len = stack.size();

        for (int i = 0; i < len; i++) {

            data2[i] = stack.pop();
        }

        var reversed = new String(data2);
        
        return original.equals(reversed);
    }
}

The example uses the Java Stack container to build a reversed string.

char[] data = original.toCharArray();

First, we turn the string to an array of characters with toCharArray.

Stack<Character> stack = new Stack<>();

for (char c: data) {

    stack.push(c);
}

In the second step, we push the characters to the stack.

char[] data2 = new char[data.length];

This array will hold the reversed characters.

for (int i = 0; i < len; i++) {

    data2[i] = stack.pop();
}

We get the reversed string now by popping the characters from the stack.

var reversed = new String(data2);
    
return original.equals(reversed);

We create the reversed string from the array and compare it with the original string using equals.

In this tutorial, we have check if a string is a palindrome. We have created various algorithms to check a palindrome.

List all Java tutorials.