KnowledgeBoat Logo

Computer Science

Write a program to read a string / sentence and output the number of times each word occurs in the entire text. At the end, the output should be sorted into ascending order words along with usage of words. You may assume that the entire text is in capitals (you may also convert in capitals for your betterment) and each word is followed by a blank space except the last one, which is followed by a full stop. Let there be at the most 50 words in the text.

Test your program for the given sample data and also some other random data:

SAMPLE DATA:

Input:
YOU ARE GOOD WHEN YOUR THOUGHTS ARE GOOD AND YOUR DEEDS ARE GOOD.

Output:

WordsWord Count
And1
Are3
Deeds1
Good3
Thoughts1
When1
You1
Your2

Input:
IF YOU FAIL TO PLAN YOU ARE PLANNING TO FAIL.

Output:

WordsWord Count
Are1
Fail2
If1
Plan1
Planning1
To2
You2

Java

Java String Handling

9 Likes

Answer

import java.util.*;

public class KboatWordFreq
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a string:");
        String str = in.nextLine();
        str = str.toUpperCase();
        int len = str.length();
        
        StringTokenizer st = new StringTokenizer(str.substring(0, len - 1));
        int wordCount = st.countTokens();
        String strArr[] = new String[wordCount];
        
        if (wordCount > 50) {
            System.out.println("Invalid Input!");
            System.out.println("Number of words should be upto 50 only.");
            return;
        }
        
        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].compareTo(strArr[j+1]) > 0) {
                    String t = strArr[j];
                    strArr[j] = strArr[j+1];
                    strArr[j+1] = t;
                }
            }
        }
        
        System.out.println(); //To Print a blank line
        System.out.println("Word\t\tWord Count");
        int count = 0;
        for (int i = 0; i < wordCount - 1; i++) {
            count++;
            if (!strArr[i].equals(strArr[i + 1])) {
                System.out.println(strArr[i] + "\t\t" + count);
                count = 0;
            }
        }
        
        //Print last word of array
        count++;
        System.out.println(strArr[wordCount - 1] + "\t\t" + count);
    }
}

Output

BlueJ output of Write a program to read a string / sentence and output the number of times each word occurs in the entire text. At the end, the output should be sorted into ascending order words along with usage of words. You may assume that the entire text is in capitals (you may also convert in capitals for your betterment) and each word is followed by a blank space except the last one, which is followed by a full stop. Let there be at the most 50 words in the text. Test your program for the given sample data and also some other random data: SAMPLE DATA: Input: YOU ARE GOOD WHEN YOUR THOUGHTS ARE GOOD AND YOUR DEEDS ARE GOOD. Output: Input: IF YOU FAIL TO PLAN YOU ARE PLANNING TO FAIL. Output:BlueJ output of Write a program to read a string / sentence and output the number of times each word occurs in the entire text. At the end, the output should be sorted into ascending order words along with usage of words. You may assume that the entire text is in capitals (you may also convert in capitals for your betterment) and each word is followed by a blank space except the last one, which is followed by a full stop. Let there be at the most 50 words in the text. Test your program for the given sample data and also some other random data: SAMPLE DATA: Input: YOU ARE GOOD WHEN YOUR THOUGHTS ARE GOOD AND YOUR DEEDS ARE GOOD. Output: Input: IF YOU FAIL TO PLAN YOU ARE PLANNING TO FAIL. Output:

Answered By

5 Likes


Related Questions