Write a method called printPalindrome that accepts a Scanner for the console as a parameter, and prompts the user to enter one or more words and prints whether the entered String is a palindrome (i.e., reads the same forwards as it does backwards, like "abba" or "racecar"). If the following Scanner object were declared: Scanner console = new Scanner(System.in); printPalindrome(console); The resulting output for a call where the user types a palindrome would be: Type one or more words: racecar racecar is a palindrome! The output for a call where the user types a word that is not a palindrome would be: Type one or more words: hello hello is not a palindrome.

Respuesta :

Answer:

public static void printPalindrome(Scanner console) {

System.out.print("Type one or more words: ");

String word = console.next();

String comp="";

String word2=word.toLowerCase();

for(int i=word2.length()-1;i>=0;i--){

comp+=word2.charAt(i);

}

System.out.println();

if(comp.equals(word2)){

System.out.println( word + " is a palindrome!");

}else{

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

}

}

Explanation:

Here is the code!

Answer:

Here's the code in Java with appropriate comments

Explanation:

public static void printPalindrome(Scanner console) {

System.out.println("Word please! ");

String s = console.next();

//checking the boolean expression if true

boolean isPalendrome = true;

for (int i = 0; i < s.length() / 2; i++) {

char p = s.charAt(i);

char q = s.charAt(s.length() - i - 1);

//applying an else conditional

if (p != q) {

isPalendrome = false;

break;

}

}

//printing if we check and evaluate it to be a palindrome

if (isPalendrome)

System.out.println("Palindrome detected!");

}

Source(s):

Sun Certified Java Programmer 5.0

ACCESS MORE