Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

1) Prompt the user to enter a word or phrase to be checked. 2) Read the input from the keyboard with nextLine (), and store

image text in transcribedimage text in transcribedimage text in transcribed

1) Prompt the user to enter a word or phrase to be checked. 2) Read the input from the keyboard with nextLine (), and store it in a String variable. 3) Set up a boolean flag variable that will indicate whether the word is a palindrome. Name it appropriately and set its initial value. Start the algorithm with the assumption that it is a palindrome, and then later update it to false when you find mismatched characters. 4) Set up a counter variable that is going to help walk through the word from back to front. Name it appropriately and initialize to the correct starting value. 5) Set up a loop to go through all the characters of the word from front to back. Decide what kind of loop you are going to use and what the starting value for the counter variable. Make sure you name your counter variable appropriately. 6) In the body of the loop: a) Get the character from the front of the word with the help of the loop counter and store it in a variable Hint: use substring or charAt but be mindful of the return type for later use b) Get the character from the back of the word with the help of the second counter and store it in a variable Hint: use the same approach(substring or charAt) as in the step above c) Print on one line the front character followed by a space Hint: if you work with chars instead of Strings, you cannot use + to join characters. Joining them to other strings works with +, however, to join 2 characters you must first convert each to a string with String.valueof (char) and then join them. d) Continue printing on the same line the "=" or "!", depending on whether the front and back characters are the same. Hint: the test depends on your approach in the steps 6a and 6b: for Strings use .equals(); for chars use == e) Use this test to see if the word still qualifies as a palindrome. Modify the value of your boolean flag variable appropriately. f) On the same line, print a space and finally the back character. FOCUS TOPICS Loops String manipulation Booleans TASK Your task is to take an input String and determine whether it meets the criteria for a palindrome. For the purposes of this lab, a palindrome is a word or phrase that reads the same backward or forward. Your input String can consists of any characters, including spaces. To be a valid palindrome for this lab, the characters must match exactly reading backward or forward, including spaces and punctuation. Some examples of words and phrases that meet and do not meet this criterion: Not a palindrome 152 98999 alb Palindrome 151 99999 ala madam esse able was i ere i saw elba radar 1234567890987654321 Oh no!on ho Hello, worlddlrow ,olleH racecar madama Esse able was i ere e saw elba rader 1234567800987654321 | Olala hellolle race car As input, let the user enter a word or phrase. You can expect that the input word contains only printable characters, and contains no leading or trailing spaces. o o o the first column has the original word, printed from top to bottom the third column has the word printed backwards the middle column has either a "=" character or "!" character printed in each row, depending on whether the first and third column characters are identical use a space to separate the columns visually, so there is a space between the first and the middle column and between the middle column and third column finally, at the end of the printout, report whether or not you found the word to be a palindrome o o Additional requirements: 0 You may use the char type and charAt() method, or you may use String's substring method to get the individual characters. Whichever way you decide, be mindful of the return type and the things you can and cannot do with it. o Do not use break or continue anywhere in the code, use a boolean flag variable instead. o If you decide to use a for loop, do not short-circuit it by changing the for's counter variable within the loop. o If you decide to use a for loop, do not use two counters inside the for statement

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions