Question #12606

Write a programme to enter a sentence ;read the entered sentence which terminates with a full stop.the words are to be seperated with a single blank space and are in lower case.arrange the words contained in the sentence according to the length of the word in ascending order.if two words are of same length then the word occuring first in the entered sentence should come first.both input and output sentence must begi with upper case.

Expert's answer

import java.util.Scanner;

public class q12606 {
public static void
main(String args[]) {
Scanner in = new Scanner(System.in);
String
input = in.nextLine();
String [] splitted = input.toLowerCase().split("
");
int maxLength = 0;
for (int i = 0; i < splitted.length;
i++)
if (splitted[i].length() > maxLength)
maxLength =
splitted[i].length();
String output = "";
for (int i = 1; i <=
maxLength; i++)
for (int j = 0; j < splitted.length; j++)

if (splitted[j].length() == i)
output += splitted[j] + " ";

output = Character.toUpperCase(output.charAt(0)) + output.substring(1);

System.out.println(output);
}
}
LATEST TUTORIALS
APPROVED BY CLIENTS