Interview Questions

193. How to find duplicate words in a string and their frequency

Microsoft Interview Questions and Answers


(Continued from previous question...)

193. How to find duplicate words in a string and their frequency

Question:
How to find duplicate words in a string and their frequency


maybe an answer:


class print
{
public:
void operator()(pair {
if(word.second > 1)
cout << "word: " << word.first << " : " << word.second << "\n";
}
};

void main()
{
string s = "test str this is test str";
map<string, int> dic;
stringstream sstr(s);

while(sstr.good())
{
string str("");
sstr >> str;
dic[str]++;
}

for_each(dic.begin(), dic.end(), print()); }

(Continued on next question...)

Other Interview Questions