Java Program To Check Palindrome Using Queue
- June 24, 2020
- by

Java Program To Check Palindrome Using Queue
In this Java Program, we check palindrome using queue.
A word, phrase, number or sequence of words that reads same backward as forward are known as palindromes.
E.g. madam, civic, level, refer, 121 1331 etc.
A linear structure which follows a particular order in which the operations are performed is known as Queue.
It follows the First In First Out (FIFO) Order.
E.g. Queue of consumers for a resource where the consumers that came first will be served first.
Logic :
Step 1 : Import java.util.Queue
Step 2 : Import java.util.Scanner
Step 3 : Import java.util.LinkedList
Step 4 : Ask the user to enter the String by using the Scanner.
Step 5 : Create variable inputString and store the user entered string into it. The Data Type of the variable should be String as we are taking input from the user in the form of string.
Step 6 : Create a Linked List and store it in the Queue.
Step 7 : Apply the for loop
Step 8 : Reverse the String
Step 9 : Apply While loop
Step 10 : Display the output by applying the if else condition.
Program :
import java.util.Queue;
import java.util.Scanner;
import java.util.LinkedList;
public class PalindromeQueue
{
public static void main(String[] args)
{
System.out.print("Enter any String:");
Scanner sc = new Scanner(System.in);
String inputString = sc.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 :
Enter any String : abccba
abccba
The input String is a Palindrome.
Enter any String : abc
abc
The input String is not a Palindrome.
For execution of the same do visit my YouTube Channel :

0 comments:
Post a Comment