Q:

Write a Java Program to Count Frequency or Occurrance of each Word in String

belongs to collection: Java String Solved Programs

0

Write a Java Program to Count Frequency or Occurrance of each Word in String. Here’s a Simple Program To Count Frequency of words in String in Java Programming Language.

A word is a sequence of one or more non-space character i.e. any character other than ” (empty String). This should be your method signature .In order to implement this method we need to assume that two words are separated by space.

 
 

We also need to ignore leading, trailing and multiple spaces between words. One way to solve this problem is  to split String by space and then count number of parts.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

ALGORITHM :

1. Start

2. Accept a sentence from the user.

3. Extract a Word.

4. Count the no. of times it occurs in the sentence.

5. Print its frequency.

6. Repeat Steps 3 to 6 till all the frequencies are printed.

7. End


Following Java Program ask to the user to enter a string to find the frequency of all the words present in the string and display the frequency of all the words one by one on the screen.

Below is the source code for Java Program to Count Frequency or Occurrance of each Word in String which is successfully compiled and run on Windows System to produce desired output as shown below :

 

SOURCE CODE : :

import java.util.Scanner;

public class Count_Words
{

    public static void main(String[] args)
    {       
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any sentence below : ");
        String text = sc.nextLine();
        String[] keys = text.split(" ");
        String[] uniqueKeys;
        int count = 0;
        
        uniqueKeys = getUniqueKeys(keys);

        for(String key: uniqueKeys)
        {
            if(null == key)
            {
                break;
            }           
            for(String s : keys)
            {
                if(key.equals(s))
                {
                    count++;
                }               
            }
            System.out.println("Count of [ "+key+" ] is : "+count);
            count=0;
        }
    }

    private static String[] getUniqueKeys(String[] keys)
    {
        String[] uniqueKeys = new String[keys.length];

        uniqueKeys[0] = keys[0];
        int uniqueKeyIndex = 1;
        boolean keyAlreadyExists = false;

        for(int i=1; i<keys.length ; i++)
        {
            for(int j=0; j<=uniqueKeyIndex; j++)
            {
                if(keys[i].equals(uniqueKeys[j]))
                {
                    keyAlreadyExists = true;
                }
            }           

            if(!keyAlreadyExists)
            {
                uniqueKeys[uniqueKeyIndex] = keys[i];
                uniqueKeyIndex++;               
            }
            keyAlreadyExists = false;
        }       
        return uniqueKeys;
    }
}

OUTPUT : :


Enter any sentence below : 

This is programming website whose name is CodezClub

Count of [ This ] is : 1
Count of [ is ] is : 2
Count of [ programming ] is : 1
Count of [ website ] is : 1
Count of [ whose ] is : 1
Count of [ name ] is : 1
Count of [ CodezClub ] is : 1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java String Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
write java program to convert the first character ... >>
<< Java Program to Count Frequency of Character in St...