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

By | March 17, 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 : :



OUTPUT : :


Leave a Reply