Tuesday , 10 December 2019
Home » Class 12 » Java program to arrange sentences in a Paragraph in Ascending order of their Number of words

Java program to arrange sentences in a Paragraph in Ascending order of their Number of words

Question:

Accept a paragraph of text consisting of sentences that are terminated by either ‘.’ (full stop), ‘!’ (exclamation mark) or a ‘?’ (question mark). Assume that there can be maximum 10 sentences in a paragraph. Write a program to arrange the sentences in increasing order of their number of words.

Example :
INPUT : Please come and attend the party. Hello! How are you?
OUTPUT :
Hello = 1
How are you = 3
Please come and attend the party = 6

Programming Code:

/**
* The class sortParagraph inputs a paragraph and arranges the 
* sentences in ascending order of their number of words
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.*;
class sortParagraph
{
    // Function to count no. of words in every sentence
    int countWords(String s)
    {
        StringTokenizer str = new StringTokenizer(s," .,?!");
        int c = str.countTokens();
        return c;
    } 
    
    // Function to sort the sentences in ascending order of their no. of words
    void sort(String w[], int p[]) 
    {
        int n = w.length, t1 = 0;
        String t2 = "";
        
        for(int i=0; ip[j]) // for descending use p[i] 10)
            System.out.println("A maximum of 10 sentences are allowed in the paragraph");
        else
        {
            String sent[] = new String[count]; //Array to store the sentences separately
            int p[] = new int[count]; //Array to store no. of words of each sentence
            
            for(int i=0; i

Output:

Enter a paragraph : Please come and attend the party. Hello! How are you?
OUTPUT :
Hello = 1
How are you = 3
Please come and attend the party = 6

Check Also

Infix Postfix Prefix Conversion – Correct Method (ISC Computer Science)

In this video we discuss about the correct way of converting an Infix Expression to Postfix and Prefix.

Leave a Reply

Your email address will not be published. Required fields are marked *