Ask Question

Write a void method called palindromeCheck that takes NO argument. The method should have functionality to check whether or not the word is a palindrome and print to the screen all of the palindromes, one per line. Also, the last line of the output should have the message: "There are x palindromes out of y words provided by user" (where x is the number of palindrome words detected, and y is the total number of words entered by user). Hint: for this lab exercise you will need the following methods on String objects: length () gives the length of a string (that is, the number of characters it contains) and charAt (i) - gives the character at position i.

+3
Answers (1)
  1. 26 January, 12:34
    0
    import java. util. Scanner;

    public class PalindromeCheck

    {

    public static void palindromeCheck ()

    {

    String someWord=" ";

    int count=0;

    int total=0;

    System. out. println ("Ënter some words entered by whitespace");

    Scanner keyboard = new Scanner (System. in);

    String[] words=keyboard. nextLine (). split (" ");

    for (int i=0; i
    {

    boolean flag=true;

    int l=words[i]. length ();

    for (int j=0; j<=l/2; j++)

    {

    if (words[i]. charAt (j) !=words[i]. charAt (l-j-1))

    {

    flag=false;

    break;

    }

    }

    if (flag)

    count++;

    }

    System. out. println ("There are "+count+" palindromes out of "+words. length+" words");

    keyboard. close ();

    }

    public static void main (String[] args)

    {

    palindromeCheck ();

    }

    }

    Output

    Ënter some words entered by whitespace

    This is a malayalam.

    There are 3 palindromes out of 4 words
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a void method called palindromeCheck that takes NO argument. The method should have functionality to check whether or not the word is ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers