Answer to Question #12606 in Java | JSP | JSF for Richa Prasad

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.
1
Expert's answer
2012-08-09T07:37:56-0400
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);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS