java A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome. Ex: If the input is: bob the output is: bob is a palindrome

Respuesta :

ijeggs

Answer:

import java.util.Scanner;

public class num8 {

   public static boolean isPali(String word)

   {

       int i = 0, j = word.length() - 1;

       // compare characters

       while (i < j) {

           // check for match

           if (word.charAt(i) != word.charAt(j))

               return false;

           i++;

           j--;

       }

       return true;

   }

   // Main Method

   public static void main(String[] args)

   {

       Scanner in = new Scanner(System.in);

       System.out.println("Enter a word to test for palidrone");

       String str = in.nextLine();

       if (isPali(str))

           System.out.print(str+" is Palidrone");

       else

           System.out.print(str+ "is not palidrone");

   }

}

Explanation:

  • Create a method that returns a boolean called isPali().
  • Using a while statement check if if (word.charAt(i) != word.charAt(j)) given that i = starts at 0 and j starts at last character in the word. If there is match for each character, return true else return false
  • In the main method use the scanner class to prompt user for a word
  • call the method isPali() and pass the word entered as argument.