Computer Science, asked by abhiaekhdutta98, 3 months ago

Using scanner class write a program to enter a line ending with space and terminating with a full stop. Find the smallest word and its number of characters present in the word .please give me the answer first because exam are nearby

Answers

Answered by anindyaadhikari13
1

Answer:

The given códe is written in Java.

import java.util.*;

public class Java {

 public static void main(String[] args) {

    String s, arr[], max="";

    int len=0, i;

    Scanner sc=new Scanner(System.in);

    System.out.print("Enter a sentence: ");

    s=sc.nextLine().trim();

    if(s.charAt(s.length()-1)=='.')

      s=s.substring(0,s.length()-1);

    arr=s.split(" ");

    for(i=0;i<arr.length;i++)  {

       if(arr[i].length()>len)  {

          len=arr[i].length();

          max=arr[i];

       }

    }

    System.out.println("Largest word is: "+max);

    sc.close();

 }

}

Algorithm:

  1. START.
  2. Accept the sentence.
  3. Split the sentence into words and store them in array.
  4. Find the largest word from the array.
  5. Display the word.
  6. STOP.

See the attachment for output ☑.

Attachments:
Similar questions