Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***JAVA****** we presented a program that checks a string to see if the letters in the string read the same forward and backward. These strings

***JAVA******

we presented a program that checks a string to see if the letters in the string read the same forward and backward. These strings are called palindromes. Another kind of palindrome is one in which we look at words rather than letters. A word-by-word palindrome is a string of words such hat the words read the same forward and backward. For example, the quote "You can cage a swallow, can't you, but you can't swallow a cage, can you?" is a word-by-word palindrome.

Write a method named isWordPalindrome in a class named PalindromeChecker with the following signature for the method.

public boolean isWordPalindrome(String input) { \* *\ }

Consider upper- and lower-case letters to be the same letters. Define a word as any string consisting of only letters or an apostrophe and bounded at each end with one of the following: a space, a punctuation mark, the beginning of the line, or the end of the line.

//========================= File: Palindrome.java ====================//

// File: Palindrome.java

// This program determines whether an input line is a palindrome. The program

// ignores everything except alphabetic letters, and it ignores the difference

// between upper- and lowercase letters.

import java.util.LinkedList;

import java.util.Queue;

import java.util.Stack;

import java.util.Scanner; // From Appendix B

/******************************************************************************

* The Palindrome Java application illustrates the use of a stack

* and a queue in the same program to determine whether a line is a

* palindrome. The program ignores everything except alphabetic letters, and it

* ignores the difference between upper- and lowercase letters.

*

*

Java Source Code for this class:

*

* http://www.cs.colorado.edu/~main/applications/Palindrome.java

*

*

* @author Michael Main

* (main@colorado.edu)

*

* @version Feb 10, 2016

******************************************************************************/

public class Palindrome

{

/**

* The main method prompts the user for a strings. Each string is read

* and checked to see whether it is a palindrome. The program ends when

* the user enters an empty line (just the return key).

* @param args

* not used in this implementation

**/

public static void main(String[ ] args)

{

Scanner stdin = new Scanner(System.in); // Keyboard input

String line; // One input line

do

{

System.out.print("Your expression (or return to end): ");

line = stdin.nextLine( );

if (is_palindrome(line))

System.out.println("That is a palindrome.");

else

System.out.println("That is not a palindrome.");

}

while (line.length( ) != 0);

}

/**

* Determine whether a string is a palindrom. Note: All non-letters are

* ignored, and the case of the letters is also ignored.

* @param input

* the string to check

* @return

* true if and only if the input string is a palindrome.

**/

public static boolean is_palindrome(String input)

{

Queue q = new LinkedList( );

Stack s = new Stack( );

Character letter; // One character from the input string

int mismatches = 0; // Number of spots that mismatched

int i; // Index for the input string

for (i = 0; i < input.length( ); i++)

{

letter = input.charAt(i);

if (Character.isLetter(letter))

{

q.add(letter);

s.push(letter);

}

}

while (!q.isEmpty( ))

{

if (q.remove( ) != s.pop( ))

mismatches++;

}

// If there were no mismatches, then the string was a palindrome.

return (mismatches == 0);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago