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

By | 17.03.2017

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


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.


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
3.7 6 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
kanu priya

Thank you so much it clearly demonstrate each and ever step.it helps me a lot

Last edited 16 days ago by kanu priya