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
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:
- START.
- Accept the sentence.
- Split the sentence into words and store them in array.
- Find the largest word from the array.
- Display the word.
- STOP.
See the attachment for output ☑.
Attachments:
Similar questions