Java program to check palindrome string

Learn to check if a given string is palindrome string with simple java programs using stack, queue or simple loops. In simplest words, a string is palindrome if it is equal to it’s reverse string.

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.

1. Algorithm

1.1. First approach

To check palindrome, we can pick the characters (one by one) from start and end of string and start comparing to each other.

  • Pick first character and last character of string and compare. If both matches – continue. Else string is not palindrome.
  • Pick second character from start and last, compare both. If both matches – continue. Else string is not palindrome.
  • Continue above comparisons till both characters to compare are same or consecutive to each other.

1.2. Second approach

Rather than comparing chars from start and end, we can also find the reverse string of the given string and compare both strings. If both strings are same, they are palindrome.

  • Get character array from given string
  • Build a string by iterating the array from end to beginning index
  • Optionally – remove comma, separators or other word dividers from both strings
  • Compare both strings

In this tutorial, we will see the examples of both approaches.

2. Check palindrome using reverse comparison

This method uses the first approach given above.

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {
         
        System.out.print("Enter any string : ");
        Scanner in = new Scanner(System.in);
         
        //Original string
        String origString = in.nextLine();
        int length = origString.length();
        
        boolean isPalindrome = true;  
        
        for(int beginIndex = 0; beginIndex < length; beginIndex++)
        {
        	if(origString.charAt(beginIndex) != origString.charAt(length-1-beginIndex)) {
        		System.out.println("String is not a palindrome.");
        		isPalindrome = false;
        		break;
        	}
        }
         
        if(isPalindrome) {
        	System.out.println("String is a palindrome.");
        }
    }
}
Enter any string : howtodoinjava
String is not a palindrome.

Enter any string : madam
String is a palindrome.

3. Check palindrome using StringBuilder.reverse()

StringBuilder.reverse() method is shortest way to reverse a string using library functions.

import java.util.Scanner;

public class Main 
{
	public static void main(String[] args) {
		
		System.out.print("Enter any string : ");
		Scanner in = new Scanner(System.in);
		
		//Original string
		String origString = in.nextLine();
		
		//Reverse string
		String reverseString = new StringBuilder(origString).reverse().toString();
		
		// Check palindrome string
		if (origString.equals(reverseString)) {
			System.out.println("String is a palindrome.");
		} else {
			System.out.println("String is not a palindrome.");
		}
	}
}
Enter any string : howtodoinjava
String is not a palindrome.

Enter any string : madam
String is a palindrome.

4. Check palindrome string using java.util.Stack

Using stack’s push() and pop() methods, we can build a reverse string for a given string. Then we compare both strings.

import java.util.Scanner;
import java.util.Stack;

public class Main 
{
	public static void main(String[] args) 
	{
		System.out.print("Enter any string : ");
		Scanner in = new Scanner(System.in);
		String origString = in.nextLine();

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

		//Push all chars in stack
		for (int i = 0; i < origString.length(); i++) {
			stack.push(origString.charAt(i));
		}

		String reverseString = "";

		//Pop all chars from stack one by one and build reverse string
		while (!stack.isEmpty()) {
			reverseString = reverseString + stack.pop();
		}

		//Check palindrome string
		if (origString.equals(reverseString)) {
			System.out.println("String is a palindrome.");
		} else {
			System.out.println("String is not a palindrome.");
		}
	}
}
Enter any string : howtodoinjava
String is not a palindrome.

Enter any string : racecar
String is a palindrome.

5. Check palindrome string using java.util.Queue

Using Queue’s add() and remove() methods, we can build a reverse string for a given string. Then we compare both strings.

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main 
{
	public static void main(String[] args) 
	{
		System.out.print("Enter any string : ");
		Scanner in = new Scanner(System.in);
		String origString = in.nextLine();

		Queue<Character> queue = new LinkedList<>();

        for (int i = origString.length()-1; i >=0; i--) {
            queue.add(origString.charAt(i));
        }

		String reverseString = "";

		//Pop all chars from stack one by one and build reverse string
		while (!queue.isEmpty()) {
			reverseString = reverseString + queue.remove();
		}

		//Check palindrome string
		if (origString.equals(reverseString)) {
			System.out.println("String is a palindrome.");
		} else {
			System.out.println("String is not a palindrome.");
		}
	}
}
Enter any string : howtodoinjava
String is not a palindrome.

Enter any string : racecar
String is a palindrome.

6. Check palindrome string using loops

This is simplest approach which simply iterated the char array backwards and creates the string by appending chars to produce reverse string.

import java.util.Scanner;

public class Main 
{
	public static void main(String[] args) 
	{
		System.out.print("Enter any string : ");
		Scanner in = new Scanner(System.in);
		String origString = in.nextLine();
		String reverseString = "";
		
		char[] characters = origString.toCharArray();

		for( int i = characters.length - 1 ; i >= 0 ; i-- ) {
			reverseString = reverseString + characters[i];
		}

		//Check palindrome string
		if (origString.equals(reverseString)) {
			System.out.println("String is a palindrome.");
		} else {
			System.out.println("String is not a palindrome.");
		}
	}
}
Enter any string : water
String is not a palindrome.

Enter any string : madam
String is a palindrome.

Drop me your questions related to check whether the given string is a palindrome or not in java.

Happy Learning !!

Comments

Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode