#include <iostream>
#include <map>
std::map<std::string, int> getOccurrences(std::string s) {
std::map<std::string, int> mp;
std::string word = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ') {
if (mp.find(word) == mp.end()) {
mp.insert(make_pair(word, 1));
word = "";
}
else {
mp[word]++;
word = "";
}
}
else
word += s[i];
}
if (mp.find(word) == mp.end()) {
mp.insert(make_pair(word, 1));
}
else
mp[word]++;
return mp;
}
int main()
{
std::string s = "c++ php c java c++ python c# c c java";
std::map<std::string, int> mp = getOccurrences(s);
for (auto& it : mp) {
std::cout << it.first << " - " << it.second << "\n";
}
return 0;
}
/*
run:
c - 3
c# - 1
c++ - 2
java - 2
php - 1
python - 1
*/