////////////////////////////////////////////////////////////////// // // wordcounter.cpp // // Counts and reports the number of times each word in // the given input file occurs, and puts the report in // the given output file. // // Also // // REVISION HISTORY: // 2/10/01 (Jeff Ondich) Wrote the original version. // 2/27/01 (JO) // 9/16/02 (JO) Much simplified. // ////////////////////////////////////////////////////////////////// #include #include #include void CleanWord( string& theWord ); int main() { map wordMap; string word; // Collect the data. Question--is wordMap[word] guaranteed to be // initialized to 0? while( cin >> word ) { CleanWord( word ); wordMap[word]++; } // Print, sorted by word map::iterator it; for( it=wordMap.begin(); it != wordMap.end(); ++it ) cout << (*it).first << '\t' << (*it).second << endl; // Print a separator cout << "\n\n=====================\n\n"; // Print, sorted by count. We use multimap here to sort by count. multimap countMap; for( it=wordMap.begin(); it != wordMap.end(); ++it ) countMap.insert(pair((*it).second,(*it).first)); multimap::reverse_iterator mmIt; for( mmIt=countMap.rbegin(); mmIt != countMap.rend(); ++mmIt ) cout << (*mmIt).second << '\t' << (*mmIt).first << endl; return 0; } ////////////////////////////////////////////////////////////////// // // CleanWord removes all non-alphabetic characters // from the given string (except for hyphens and // apostrophes), and reduces all upper case letters // to lower case. Thus, once CleanWord is done, theWord // consists entirely of lower case letters, hyphens, // and apostrophes. // ////////////////////////////////////////////////////////////////// void CleanWord( string& theWord ) { string s = ""; int originalLength = theWord.length(); for( int i=0; i < originalLength; i++ ) { char c = theWord[i]; if( isalpha(c) || c == '\'' || c == '-' ) s += tolower(c); } theWord = s; }