Tuesday, 5 April 2016

C++ Program to Count number of Words in String display the words and number of times it count problem Solution.


Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    map<string,int>Container;
    map<string,int>::iterator it;
    string str,tmp;

    cout << "Enter a String: ";
    getline(cin,str);

    for(int i = 0; i < str.size(); i++){
        if(str[i] == ' '){
            if(!tmp.empty() && tmp != ".")
                Container[tmp]++;
            tmp.clear();
        }
        else if(str[i] == '.'){
            if(!tmp.empty() && tmp != ".")
                Container[tmp]++;
            tmp.clear();
        }
        else if(str[i] != ',') tmp += str[i];
    }
    if(!tmp.empty() && tmp != ".")Container[tmp]++;

    for(it = Container.begin(); it != Container.end(); it++)cout << it->first << " " << it->second << endl;
    return 0;
}

No comments:

Post a Comment