home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / basedemo / wordcnt / wordcnt.cxx
C/C++ Source or Header  |  1994-10-14  |  913b  |  35 lines

  1.  
  2. // A program for counting the number of occurrences of each word in the
  3. // input file.
  4. //
  5. // M. A. Sridhar
  6. // July 29, 1994
  7.  
  8.  
  9. #include "base/base.h"
  10. #include <iostream.h>
  11. main ()
  12. {
  13.     CL_String line;
  14.     CL_StringIntMap word_map;
  15.     long count = 0;
  16.     const char* punctuation = " :~{}.();&,!?";
  17.     while (line.ReadLine (cin)) {
  18.         CL_StringSequence words = line.Split (punctuation);
  19.         register long n = words.Size();
  20.         for (register long i = 0; i < n; i++) {
  21.             if (word_map.IncludesKey (words[i]))
  22.                 word_map[words[i]]++;
  23.             else
  24.                 word_map.Add (words[i], 1);
  25.         }
  26.     }
  27.     CL_StringIntMapIterator itr (word_map);
  28.     CL_StringIntAssoc assoc;
  29.     for (itr.Reset (); itr.More (); ) {
  30.         assoc = itr.Next();
  31.         cout << assoc.key << " " << assoc.value << endl;
  32.     }
  33.     cout << "Total " << word_map.Size() << " words." << endl;
  34. }
  35.