KnowledgeBoat Logo

Computer Science

Read a single sentence which terminates with a full stop (.). The words are to be separated with a single blank space and are in lower case. Arrange the words contained in the sentence according to the length of the words in ascending order. If two words are of the same length then the word occurring first in the input sentence should come first. For both, input and output the sentence must begin in upper case.

Test your program for given data and also some random data:

Input:
The lines are printed in reverse order.
Output:
In the are lines order printed reverse.

Input:
Print the sentence in ascending order.
Output:
In the print order sentence ascending.

Input:
I love my Country.
Output:
I my love Country.

Java

Java String Handling

20 Likes

Answer

import java.util.*;

public class KboatWordLenSort
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a sentence:");
        String str = in.nextLine();
        int len = str.length();
        
        if (str.charAt(len - 1) != '.') {
            System.out.println("Invalid Input!");
            System.out.println("Sentence should end with full stop.");
            return;
        }
        
        if (Character.isLowerCase(str.charAt(0))) {
            System.out.println("Invalid Input!");
            System.out.println("Sentence should start with upper case letter.");
            return;
        }
        
        String ipStr = Character.toLowerCase(str.charAt(0)) + str.substring(1, len - 1);
        StringTokenizer st = new StringTokenizer(ipStr);
        int wordCount = st.countTokens();
        String strArr[] = new String[wordCount];
        
        for (int i = 0; i < wordCount; i++) {
            strArr[i] = st.nextToken();
        }
        
        for (int i = 0; i < wordCount - 1; i++) {
            for (int j = 0; j < wordCount - i - 1; j++) {
                if (strArr[j].length() > strArr[j + 1].length()) {
                    String t = strArr[j];
                    strArr[j] = strArr[j+1];
                    strArr[j+1] = t;
                }
            }
        }
        
        strArr[0] = Character.toUpperCase(strArr[0].charAt(0))
                    + strArr[0].substring(1);
                    
        System.out.println("Sorted String:");
        for (int i = 0; i < wordCount; i++) {
            System.out.print(strArr[i]);
            if (i == wordCount - 1) {
                System.out.print(".");
            }
            else {
                System.out.print(" ");
            }
        }
    }
}

Output

BlueJ output of Read a single sentence which terminates with a full stop (.). The words are to be separated with a single blank space and are in lower case. Arrange the words contained in the sentence according to the length of the words in ascending order. If two words are of the same length then the word occurring first in the input sentence should come first. For both, input and output the sentence must begin in upper case. Test your program for given data and also some random data: Input: The lines are printed in reverse order. Output: In the are lines order printed reverse. Input: Print the sentence in ascending order. Output: In the print order sentence ascending. Input: I love my Country. Output: I my love Country.BlueJ output of Read a single sentence which terminates with a full stop (.). The words are to be separated with a single blank space and are in lower case. Arrange the words contained in the sentence according to the length of the words in ascending order. If two words are of the same length then the word occurring first in the input sentence should come first. For both, input and output the sentence must begin in upper case. Test your program for given data and also some random data: Input: The lines are printed in reverse order. Output: In the are lines order printed reverse. Input: Print the sentence in ascending order. Output: In the print order sentence ascending. Input: I love my Country. Output: I my love Country.BlueJ output of Read a single sentence which terminates with a full stop (.). The words are to be separated with a single blank space and are in lower case. Arrange the words contained in the sentence according to the length of the words in ascending order. If two words are of the same length then the word occurring first in the input sentence should come first. For both, input and output the sentence must begin in upper case. Test your program for given data and also some random data: Input: The lines are printed in reverse order. Output: In the are lines order printed reverse. Input: Print the sentence in ascending order. Output: In the print order sentence ascending. Input: I love my Country. Output: I my love Country.

Answered By

6 Likes


Related Questions