Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main() {
- cout << "Enter some words: ";
- vector<string> words;
- string w;
- while(cin >> w) {
- words.push_back(w);
- }
- sort(words.begin(), words.end());
- int count = 1;
- int most_so_far = 0;
- vector<string> most_frequent;
- for(int i = 0; i < words.size(); ++i) {
- if(words[i] == words[i+1]) {
- ++count;
- }
- else if(words[i] != words[i+1]) {
- if(count > most_so_far) {
- most_so_far = count;
- most_frequent.clear();
- most_frequent.push_back(words[i]);
- count = 1;
- }
- else if(count == most_so_far) {
- most_frequent.push_back(words[i]);
- count = 1;
- }
- else { //count < most_so_far
- count = 1;
- }
- }
- }
- sort(most_frequent.begin(), most_frequent.end());
- cout << "The most frequently occuring words are: \n\n";
- for(int i = 0; i < most_frequent.size(); ++i) {
- cout << most_so_far << " " << most_frequent[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment