Question
Complete the printPalindrome method in the provided Palindrome.java program. The printPalindrome method accepts a String as a parameter and prints whether the parameter String is
Complete the printPalindrome method in the provided Palindrome.java program.
The printPalindrome method accepts a String as a parameter and prints whether the parameter String is a palindrome (i.e., reads the same forwards as it does backwards, for example, "abba" or "racecar").
Make the code case-insensitive, so that words like "Abba" and "Madam" will be considered palindromes.
import java.util.*;
/** * Determines if a user entered String is a palindrome, which means the String * is the same forward and reverse. The determination is case-insensitive. * Spaces and punctuation must match for palindromes. * */ public class Palindrome {
/** * Starts program * * @param args array of command line arguments */ public static void main(String[] args) { printPalindrome("abba");// palindrome printPalindrome("racecar");// palindrome printPalindrome("Madam");// palindrome printPalindrome("!!!Hannah!!!");// palindrome printPalindrome("! ! ! Hannah ! ! !");// palindrome printPalindrome("Dot saw I was tod");// palindrome printPalindrome("Step on no pets");// palindrome printPalindrome("Step on no pets!"); // NOT printPalindrome("dog"); // NOT printPalindrome("Java"); // NOT printPalindrome("abca"); // NOT System.out.println(" *****Read from User*****"); readConsole(); }
/** * Reads user's console input and prints the results */ public static void readConsole() { Scanner in = new Scanner(System.in); System.out.print("Enter a word or phrase: "); String text = in.nextLine(); printPalindrome(text); }
/** * Prints whether text is a palindrome * @param text String to check whether it is a palindrome */ public static void printPalindrome(String text) { System.out.print(" isPalindrome: \t"); //create a String that is the reverse of text String reverse = ""; // if text equals reversed text (remember to ignore case) then print: System.out.println("\"" + text + "\" is a palindrome"); // otherwise print: System.out.println("\"" + text + "\" is NOT a palindrome"); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started