# Calculate the frequency of each word in the given string

Given a string str, the task is to find the frequency of each word in a string.

Examples:

Input: str = “Geeks For Geeks”
Output:
For 1
Geeks 2
Explanation:
For occurs 1 time and Geeks occurs 2 times in the given string str.

Input: str = “learning to code is learning to create and innovate”
Output:
and 1
code 1
create 1
innovate 1
is 1
learning 2
to 2
Explanation:
The words and, code, create, innovate, is occurs 1 time; and learning, to occurs 2 times in the given string str.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: To solve the problem mentioned above we have to follow the steps given below:

• Use a Map data structure to store the occurrence of each word in the string.
• Traverse the entire string and check whether the current word is present in map or not. If it is present, then update the frequency of the current word else insert the word with frequency 1.
• Traverse in the map and print the frequency of each word.

Below is the implementation of the above approach:

## C++

 `// C++ program to calculate the frequency ` `// of each word in the given string ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to print frquency of each word ` `void` `printFrequency(string str) ` `{ ` `    ``map M; ` ` `  `    ``// String for storing the words ` `    ``string word = ``""``; ` ` `  `    ``for` `(``int` `i = 0; i < str.size(); i++) { ` ` `  `        ``// Check if current character ` `        ``// is blank space then it ` `        ``// means we have got one word ` `        ``if` `(str[i] == ``' '``) { ` ` `  `            ``// If the current word ` `            ``// is not found then insert ` `            ``// current word with frequency 1 ` `            ``if` `(M.find(word) == M.end()) { ` `                ``M.insert(make_pair(word, 1)); ` `                ``word = ``""``; ` `            ``} ` ` `  `            ``// update the frequency ` `            ``else` `{ ` `                ``M[word]++; ` `                ``word = ``""``; ` `            ``} ` `        ``} ` ` `  `        ``else` `            ``word += str[i]; ` `    ``} ` ` `  `    ``// Storing the last word of the string ` `    ``if` `(M.find(word) == M.end()) ` `        ``M.insert(make_pair(word, 1)); ` ` `  `    ``// Update the frequency ` `    ``else` `        ``M[word]++; ` ` `  `    ``// Traverse the map ` `    ``// to print the  frequency ` `    ``for` `(``auto``& it : M) { ` `        ``cout << it.first << ``" - "` `             ``<< it.second ` `             ``<< endl; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``string str = ``"Geeks For Geeks"``; ` ` `  `    ``printFrequency(str); ` `    ``return` `0; ` `} `

## Python3

 `# Python3 program to calculate the frequency ` `# of each word in the given string ` ` `  `# Function to prfrquency of each word ` `def` `printFrequency(strr): ` `    ``M ``=` `{} ` `     `  `    ``# string for storing the words ` `    ``word ``=` `"" ` `     `  `    ``for` `i ``in` `range``(``len``(strr)): ` `         `  `        ``# Check if current character ` `        ``# is blank space then it ` `        ``# means we have got one word ` `        ``if` `(strr[i] ``=``=` `' '``): ` `             `  `            ``# If the current word      ` `            ``# is not found then insert ` `            ``# current word with frequency 1 ` `            ``if` `(word ``not` `in` `M): ` `                ``M[word] ``=` `1` `                ``word ``=` `"" ` `             `  `            ``# update the frequency ` `            ``else``: ` `                ``M[word] ``+``=` `1` `                ``word ``=` `"" ` `         `  `        ``else``: ` `            ``word ``+``=` `strr[i] ` `     `  `    ``# Storing the last word of the string ` `    ``if` `(word ``not` `in` `M): ` `        ``M[word] ``=` `1` `     `  `    ``# Update the frequency ` `    ``else``: ` `        ``M[word] ``+``=` `1` `         `  `    ``# Traverse the map ` `    ``# to prthe frequency ` `    ``for` `it ``in` `M: ` `        ``print``(it, ``"-"``, M[it]) ` `     `  `# Driver Code ` `strr ``=` `"Geeks For Geeks"` `printFrequency(strr) ` ` `  `# This code is contributed by shubhamsingh10 `

Output:

```For - 1
Geeks - 2
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : SHUBHAMSINGH10

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.