Ask Question
23 May, 18:14

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

+1
Answers (1)
  1. 23 May, 21:47
    0
    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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “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 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers