Computer Science

Input a paragraph containing 'n' number of sentences where (1 < = n < 4). The words are to be separated with a single blank space and are in UPPERCASE. A sentence may be terminated either with a full stop '.' Or a question mark '?' only. Any other character may be ignored. Perform the following operations:

  1. Accept the number of sentences. If the number of sentences exceeds the limit, an appropriate error message must be displayed.
  2. Find the number of words in the whole paragraph.
  3. Display the words in ascending order of their frequency. Words with same frequency may appear in any order.

Example 1

Input:
Enter number of sentences.
1
Enter sentences.
TO BE OR NOT TO BE.

Output:
Total number of words: 6

WordFrequency
OR1
NOT1
TO2
BE2

Example 2

Input:
Enter number of sentences.
3
Enter sentences.
THIS IS A STRING PROGRAM. IS THIS EASY? YES IT IS.

Output:
Total number of words: 11

WordFrequency
A1
STRING1
PROGRAM1
EASY1
YES1
IT1
THIS2
IS3

Example 3

Input:
Enter number of sentences. 5

Output:
Invalid Entry

Java

Java String Handling

6 Likes

Answer

import java.util.*;

public class KboatPara
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter number of sentences.");
        int n = in.nextInt();
        in.nextLine();
        
        if (n < 1 || n > 3) {
            System.out.println("Invalid Entry");
            return;
        }
        
        System.out.println("Enter sentences.");
        String ipStr = in.nextLine();
        ipStr = ipStr.toUpperCase();
        
        StringTokenizer st = new StringTokenizer(ipStr, " .?");
        int wordCount = st.countTokens();
        
        System.out.println();
        System.out.println("Total number of words: " + wordCount);
        
        String wordArr[] = new String[wordCount];
        int wordFreq[] = new int[wordCount];
        int idx = 0;
        
        for (int i = 0; i < wordCount; i++) {
            
            String word = st.nextToken();
            int j = 0;
            
            for (j = 0; j < idx; j++) {
                if (wordArr[j].equals(word)) {
                    wordFreq[j]++;
                    break;
                }
            }
            
            if (j == idx) {
                wordArr[idx] = word;
                wordFreq[idx]++;
                idx++;
            }
        }
        
        //Sort Word Frequency in ascending order
        for (int i = 0; i < idx - 1; i++) {
            for (int j = 0; j < idx - i - 1; j++) {
                if (wordFreq[j] > wordFreq[j + 1]) {
                    int t = wordFreq[j];
                    wordFreq[j] = wordFreq[j+1];
                    wordFreq[j+1] = t;
                    
                    String temp = wordArr[j];
                    wordArr[j] = wordArr[j+1];
                    wordArr[j+1] = temp;
                }
            } 
        }
        
        //Display the words and frequencies
        System.out.println("Word\tFrequency");
        for (int i = 0; i < idx; i++) {
            System.out.println(wordArr[i] + "\t" + wordFreq[i]);
        }
    }
}

Output

Answered By

1 Like


Related Questions