BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to check Palindrome string using Stack and Queue

Last Updated: July 26, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, you will learn how to write a java program check whether the given String is Palindrome or not. There are following three ways to check for palindrome string.

1) Using Stack
2) Using Queue
3) Using for/while loop

Program 1: Palindrome check Using Stack

In this example, user enter a string. The program iterates over the input string by running a loop from 1 to the length of the string  and adds each character of the string to the stack using push() method.

Once all the characters of the given string are added to the stack. The program runs a while loop until the stack is empty and at each iteration removes the last character of the string using pop() method and adds it to the reverseString variable.

The pop() method removes the last character from the stack. In the last step compare the string and reverseString to check if the string is palindrome or not.

import java.util.Stack;
import java.util.Scanner;
class PalindromeTest {

    public static void main(String[] args) {

    	System.out.print("Enter any string:");
        Scanner in=new Scanner(System.in);
        String inputString = in.nextLine();
        Stack stack = new Stack();

        for (int i = 0; i < inputString.length(); i++) {
            stack.push(inputString.charAt(i));
        }

        String reverseString = "";

        while (!stack.isEmpty()) {
            reverseString = reverseString+stack.pop();
        }

        if (inputString.equals(reverseString))
            System.out.println("The input String is a palindrome.");
        else
            System.out.println("The input String is not a palindrome.");

    }
}

Output 1:

Enter any string:abccba
The input String is a palindrome.

Output 2:

Enter any string:abcdef
The input String is not a palindrome.

Program 2: Palindrome check Using Queue

In this example, we are using Queue to reverse the given string. Each character of the string is read by using String charAt() method. These characters are added to the Queue using add() method. Once all the characters are added. We are iterating the Queue and removing the last character of the Queue using remove() method and appending it to the reverseString.

import java.util.Queue;
import java.util.Scanner;
import java.util.LinkedList;
class PalindromeTest {

    public static void main(String[] args) {

    	System.out.print("Enter any string:");
        Scanner in=new Scanner(System.in);
        String inputString = in.nextLine();
        Queue queue = new LinkedList();

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

        String reverseString = "";

        while (!queue.isEmpty()) {
            reverseString = reverseString+queue.remove();
        }
        if (inputString.equals(reverseString))
            System.out.println("The input String is a palindrome.");
        else
            System.out.println("The input String is not a palindrome.");

    }
}

Output 1:

Enter any string:xyzzyx
xyzzyx
The input String is a palindrome.

Output 2:

Enter any string:xyz
The input String is not a palindrome.

Program 3: Using for loop/While loop and String function charAt

This is really a simple program. You just need to have a basic understanding of for loop and if..else statement to understand this program. Here, we are iterating over the string in reverse order (from length-1 to 0) and reading the corresponding character using charAt() method. These characters read in reverse order are appended to the reverseString. In the last step, similar to the above programs, the string and reverseString are compared to check if the input string is palindrome.

import java.util.Scanner;
class PalindromeTest {
   public static void main(String args[])
   {
      String reverseString="";
      Scanner scanner = new Scanner(System.in);

      System.out.println("Enter a string to check if it is a palindrome:");
      String inputString = scanner.nextLine();

      int length = inputString.length();

      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverseString = reverseString + inputString.charAt(i);

      if (inputString.equals(reverseString))
         System.out.println("Input string is a palindrome.");
      else
         System.out.println("Input string is not a palindrome.");

   }
}

Output 1:

Enter a string to check if it is a palindrome:
aabbaa
Input string is a palindrome.

Output 2:

Enter a string to check if it is a palindrome:
aaabbb
Input string is not a palindrome.

If you wanna use While Loop in above program then replace the for loop with this code:

int i = length-1;
while ( i >= 0){
    reverseString = reverseString + inputString.charAt(i);
    i--;
}

Related Java Examples:

  • Java Program to remove all the white spaces from a string
  • Java Programs to print pattern in java
  • Java Program to check two strings are anagram or not
  • Java Program to sort strings in an alphabetical order
❮ Java TutorialJava Programs ❯

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. java program to find factorial of a given number using recursion
  3. Java Program to find Sum of Natural Numbers
  4. Tech Number Program in Java
  5. Java program to perform Bubble Sort on Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Jasper says

    January 30, 2015 at 3:45 AM

    This is helpful, but you actually can’t use the push method on a char. You have to cast it as a java.lang.Character, so that it becomes an Object. For example,
    stack.push(new Character(inputString.charAt(i)));

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap