1 / 2100%
/*
* xxxxxxxx
* 4/18/2019
* palindromeFinder.java
* This project basically implements stacks and queues and sees if a string
being entered is a palindrome.
* */
/*
* output:
* Enter a string to determine if its a palindrome:
No Lemon, no Melon!!
true
*/
package module10Package;
import java.util.*;
public class PalindromeFinder {
public static void main(String[] args)
{
//instantiating a queue
Queue<Character> queue = new LinkedList<>();
//instantiating a stack
Stack<Character> stack = new Stack<>();
Scanner scan = new Scanner(System.in);
System.out.println("Enter a string to determine if its a palindrome: ");
String x = scan.nextLine();
//this takes away all characters that are not letters
x = x.replaceAll("[^A-Za-z]+","").toUpperCase();
//Population of our stack and queue
for(int y = 0 ; y < x.length(); y++ )
{
stack.push(x.charAt(y));
queue.offer(x.charAt(y));
}
//this is the call for out isPalindrome method
System.out.println(isPalindrome(queue, stack,x));
}
/*
* This method takes in a queue and stack and determine if a string is a
palindrome or not.
* @param queue1 This holds the characters of a string in a queue.
* @param stack1 This holds the characters of a string in a stack.
* @param string This is the string that is in question.
* @return boolean returns a true or a false depending on if the string is
a palindrome or not.
*/
static String isPalindrome (Queue queue1, Stack stack1, String
string)
{
Stack<Character> reverseStack = new Stack<>();
String falseString = "";
Character it;
//reverses the order of our string and stores it in another
string
for(int i = 0; i < string.length(); i++)
{
reverseStack.push(string.charAt(i));
}
for(int i =0; i < string.length(); i++)
{
it = reverseStack.pop();
falseString = falseString + it ;
}
//if the string is not a palindrome the reversed
string is returned
if(queue1.poll() != stack1.pop())
return falseString.toLowerCase();
else
return "This is a palindrome!!!";
}
}
Powered by TCPDF (www.tcpdf.org)
Students also viewed