-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathFrequency_Count
55 lines (44 loc) · 1.08 KB
/
Frequency_Count
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// frequency of each word
// in given string
#include <bits/stdc++.h>
using namespace std;
// Function to print frequency of each word
void printFrequency(string str)
{
map<string, int> M;
string word = "";
for (int i = 0; i < str.size(); i++) {
// if element is empty
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 = "";
}
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
for (auto& it : M) {
cout << it.first << " - " << it.second << endl;
}
}
int main()
{
string str = "Geeks For Geeks is for Geeks";
printFrequency(str);
return 0;
}