Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a MAL program that prompts the user for a line of text and reads the line

ID: 3573379 • Letter: W

Question

Write a MAL program that prompts the user for a line of text and reads the line typed by the user. If the line contains just white space characters your program should simply output the message "Line contains only white space characters." and stop. Otherwise, your program should computer and output the following 1) The number of non-whitespace characters in the line 2) The number of words in the line 3) The maximum length of a word in the line 4) The minimum length of a word in the line 50 The word of the maximum length in the line Tf there happens to have two or more words of maximum length, print the word that appears last in the line.) 60 The word of the minimum length in the line Tf there happens to have two or more words of minimum length, print the word that appears last in the line.) Notice: Any line of text typed by a user has at most 80 characters including the newline character A whitespace character refers to a space, a tab, or the newline character. A word is any sequence of characters that does not contain a whitespace character Example: Suppose the line typed by the user is the following: It was the best of times and it was the worst of times. The output answer is as follows: Number of non-whitespace characters: 43 Number of words: 13 Maximum length of a word: 6 Minimum length of a word: 2 Word of maximum length: times Word of minimum length: of Program outline a) Prompt the user for a line of text b) Read the line of text typed by the user c) If the line has only whitespace characters Print the message "Line contains only white space characters. else Computer the quantities mentioned above and print the answers. d) Stop.

Explanation / Answer

Import java.io.*;

class Test

{

Public static int countWords(String str) // Count the Number of words and spaces

    {

  int count = 1;

  String str="count number of words and sapces";

   for (int i=0;i<str.length();++i)

  {

   if(str.charAt(i)==' ')

    count++;

  }

}

return count;

}

}

Public static void main (String args[] throws) IOException

{

BufferedReader br= new BufferedReader(new) InputStreamReader(System.in));

System.out.print(“Enter any sentence : “); // Inputing the sentence

String s = br.readLine();

s = s+ ““; // adding a space at the end to extract the last word also

int len = s.lenghth();

String X = ““, maxw=” “, minw=” “;

Char ch;

Int p, maxl =0, minl=len;

for(int j=0;j<len;j++)

{

ch = s.chatAt(i); // Extracting character of the string one at atime

if(ch! = ‘ ‘)

{

x= x+ch; adding characters to form word if character is not having space

}

else

{

p=x.Length();

if (p<minl) //check the minimum length

{

minl=p;

minw=x;

}

if (p>maxl) // check the maximum length

{

maxl=p;

maxw=x;

}

x=””; // Empty the temporary variable to input next

}

}

System.out.println("Number of words="+count);

System.out.println("Number of spaces="+(count-1));

System.out.println(“Word of minimum length="+minw+” Length of word = “+minl);

System.out.println(“Word of minimum length="+maxw+” Length of word = “+maxl);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote