0% found this document useful (0 votes)
32 views

To Gain Experience With Stack and Queue Data Structures and Their Applications

This program uses stacks and queues to check if a string is a palindrome. It takes a string as input, converts it to lowercase and removes spaces and punctuation. It then inserts the characters of the string into both a stack and queue. It pops from the stack and dequeues from the queue, checking if the characters are equal, and returns false if any are unequal. It returns true if the while loop finishes without finding unequal characters.

Uploaded by

Torcoxk Namgay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

To Gain Experience With Stack and Queue Data Structures and Their Applications

This program uses stacks and queues to check if a string is a palindrome. It takes a string as input, converts it to lowercase and removes spaces and punctuation. It then inserts the characters of the string into both a stack and queue. It pops from the stack and dequeues from the queue, checking if the characters are equal, and returns false if any are unequal. It returns true if the while loop finishes without finding unequal characters.

Uploaded by

Torcoxk Namgay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lap Task 4

Aim: to gain experience with stack and queue data structures and their applications.

Program Code:

package package1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestPalindrome {


//this method has returns boolean and checks palindrome of string
public boolean isPalindrome(String word) throws IOException {
//converts the string into char array
char[] array = word.toCharArray();
//creates stack and queue of size equal to string length
ArrayStack<Character> s = new <Character>ArrayStack(array.length);
ArrayQueue<Character> q = new <Character>ArrayQueue(array.length);
for (int i = 0; i < array.length; i++) {
//inserts char inside stack
s.push(array[i]);
//inserts char inside queue
q.enqueue(array[i]);
}
//while loop runs and removes the elements from top and bottom and compares the elements
//if the elements are not same, it returns false
//and while loop runs till the array becomes empty
while (!q.isEmpty()) {
if (s.pop() != q.dequeue())
return false;
}
return true;

public static void main(String args[]) throws IOException {


PalindromeTest ob = new PalindromeTest();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//no. of strings to check the palindrome
//To check the palindrome for a given user input
// user input string is converted to lowercase.
//space b/w the strings are removed
//Punctuation marks b/w the strings are also removed.
int N = Integer.parseInt(br.readLine());
String str;
for (int i = 0; i < N; i++) {
str = br.readLine().toLowerCase();
str = str.replace(" ", "");
str = str.replaceAll("\\p{Punct}", "");
if (ob.isPalindrome(str)) {
//Prints "Yes" if the given string is palindrome
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
}

Outputs

You might also like