Question

In: Computer Science

1. Write a method called isPalindrome that accepts a string as a parameter and returns true...

1. Write a method called isPalindrome that accepts a string as a parameter and returns true if the string is a palindrome otherwise returns false. This method uses a stack and a Queue to test whether the given string parameter is a palindrome [ that is, whether the characters read the same both forward and backward. For example “race car”, and “Are we not drawn onward, to new era.” are Palindromes] They are palindrome sentences, not just a word. 2. If a string is a palindrome, do not print it out, just print a message that the given string is a Palindrome. If a string is not a palindrome, print the letters of the string in reverse order. 3. Your application prompts the user for the input string. 4. You must use a stack and a queue to do this. (You have to figure out how both can be used.) Any solution that does not use a stack and a queue for the palindrome checking and reversing the letters of the words of the sentence will receive a grade of 0.

Solutions

Expert Solution

Thanks for the question.

Here is the completed code for this problem. 

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer. 

Thanks
===========================================================================
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class IsPalindrome {


    // method returns true if the sentence is palindrome else returns false
    public static boolean isPalindrome(String sentence) {

        Stack<Character> stack = new Stack<Character>();
        Queue<Character> queue = new ArrayDeque<Character>();

        sentence = sentence.toUpperCase(); // convert the sentence to upper case
        char letter; // current letter in the sentence

        // iterate over each letter in the sentence add to the stack and to the queue
        for (int i = 0; i < sentence.length(); i++) {
            letter = sentence.charAt(i);
            // only add if its all alphabet
            if ('A' <= letter && letter <= 'Z') {
                stack.push(letter);
                queue.add(letter);
            }
        }
        // pop out the last element from stack and the first element from the queue
        while (!stack.isEmpty()) {
            // compare both are equal; if they are not equal then its not a palindrome
            if (stack.pop() != queue.poll()) {
                //System.out.println("Not a palindrome");
                return false;
            }
        }
        //System.out.println("Its a palindrome");
        return true;
    }

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a sentence to check if its a palindrome: ");
        String sentence = scanner.nextLine();
        boolean check = isPalindrome(sentence);
        if (check) {
            System.out.println("Its a palindrome");
        } else {
            System.out.println("Not a palindrome");
        }

    }
}

==============================================================


Related Solutions

Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',',...
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',', '!', '.') characters from the string, and returns the number of punctuation characters removed. For example, if the string contains ['C', 'p', 't', 'S', ',', '1', '2', '1', '.', 'i', 's', 'f', 'u', 'n', '!', '\0'], then the function should remove the punctuation characters. The function must remove the characters by shifting all characters to the right of each punctuation character, left by one...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT