Write a method that checks whether the input string or a sentence (a string with spaces) is a palindrome or not. The method should be case insensitive and should ignore spaces. Write a test program that prompts the user to input a string and invokes this method. Some example runs are: Enter the input string: madam Input string madam is a palindrome Enter the input string: banana Input string banana is NOT a palindrome Enter the input string: Race Car Input string Race Car is a palindrome Enter the input string: Too HOT to hoot Input string Too HOT to hoot is a palindrome

Respuesta :

ijeggs

Answer:

import java.util.Scanner;

public class Pallindrome {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       System.out.println("Enter the input string: ");

       String word = in.nextLine();

       System.out.println(isPallindrome(word));

   }

   public static String isPallindrome(String word){

       String rev ="";

       int len = word.length();

       for ( int i = len - 1; i >= 0; i-- )

           rev = rev + word.charAt(i);

       if (word.equals(rev))

           return word+" is palindrome";

       else

           return word+ " is not palindrome";

   }

}

Explanation:

  • Create the method in Java to receive a String parameter
  • Using a for loop reverse the string
  • Use another for loop to compare the characters of the original string and the reversed string
  • If they are equal print palindrome
  • else print not palindrome
  • Within the main method prompt user for a sentence
  • Call the method and pass the sentence entered by user

In this exercise we have to use the knowledge in computational language in JAVA to describe a code that best suits, so we have:

The code can be found in the attached image.

To make it simpler we can write this code as:

public class Pallindrome {

  public static void main(String[] args) {

      Scanner in = new Scanner(System.in);

      System.out.println("Enter the input string: ");

      String word = in.nextLine();

      System.out.println(isPallindrome(word));

  }

  public static String isPallindrome(String word){

      String rev ="";

      int len = word.length();

      for ( int i = len - 1; i >= 0; i-- )

          rev = rev + word.charAt(i);

      if (word.equals(rev))

          return word+" is palindrome";

      else

          return word+ " is not palindrome";

  }

}

See more about JAVA at brainly.com/question/19705654

Ver imagen lhmarianateixeira