Program opened below. You can scroll through the algorithm, Java code and output.

Arrange Sentences by Word Count

Download .java

Question

Input at most ten sentences ending with ., ! or ?. Arrange the sentences in increasing order of their word counts, and display each count.

Step 1: Start the program and read the paragraph. Step 2: Prepare arrays to store at most ten sentences and their word counts. Step 3: Initialize an empty temporary sentence. Step 4: Inspect each character of the paragraph. Step 5: Add normal characters to the temporary sentence. Step 6: When a terminator is found, trim the accumulated sentence. Step 7: Ignore empty sentences and store each nonempty sentence. Step 8: Count its words by detecting the start of each run of non-space characters. Step 9: Store the corresponding word count in a parallel array. Step 10: Clear the temporary sentence for the next input section. Step 11: Choose insertion sort to order the sentences. Step 12: Take each sentence and its word count as a pair. Step 13: Shift earlier pairs with larger word counts one position right. Step 14: Insert the selected pair in the correct position. Step 15: Display each sentence followed by its word count. Step 16: Stop.
Java
import java.util.*;
class SentenceOrder {
    static int words(String s) {
        int count = 0;
        boolean inside = false;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ' ') {
                if (!inside) {
                    count++;
                }
                inside = true;
            } else {
                inside = false;
            }
        }
        return count;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String text = sc.nextLine();
        String[] sentences = new String[10];
        int[] totals = new int[10];
        int size = 0;
        String part = "";
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (ch == '.' || ch == '!' || ch == '?') {
                if (part.trim().length() > 0 && size < 10) {
                    sentences[size] = part.trim();
                    totals[size] = words(sentences[size]);
                    size++;
                }
                part = "";
            } else {
                part += ch;
            }
        }
        // Stable insertion sort by word count.
        for (int i = 1; i < size; i++) {
            String item = sentences[i];
            int total = totals[i];
            int j = i - 1;
            while (j >= 0 && totals[j] > total) {
                sentences[j + 1] = sentences[j];
                totals[j + 1] = totals[j];
                j--;
            }
            sentences[j + 1] = item;
            totals[j + 1] = total;
        }
        for (int i = 0; i < size; i++) {
            System.out.println(sentences[i] + " = " + totals[i]);
        }
    }
}
Input: Whose dog is this? Oh! It is yours. Output: Oh = 1 / It is yours = 3 / Whose dog is this = 4
← Previous Program 5 of 84 Next →

Share a comment or request a program

Use this form to send a comment, report an issue, or suggest a new practical. Your message will be saved for review by the school.