By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,215 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

word count problem

P: 3
can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of characters, words, and lines but i need the occurance of each word.

Expand|Select|Wrap|Line Numbers
  1. #include <fstream>  
  2. #include <iostream> 
  3. #include <string>   
  4. #include <cstdlib> 
  5.  
  6. using namespace std; 
  7.  
  8.  
  9.  
  10. string getInputFileName(); // a function to prompt for the complete file name
  11.  
  12. int numCharsInFile( ifstream &in, int &numLines ); // a function to count the
  13.                                                    //    number of characters and
  14.                                                    //    lines in a text file
  15.  
  16. int numWordsInFile( ifstream &in, int &numWords ); // a function to count words in file
  17.  
  18.  
  19.  
  20.  main ()
  21. {
  22.  
  23.     char c;
  24.   int nLines,          // number of lines in the text file
  25.       nChars,          // number of characters in the text file
  26.       avgCharsPerLine, // average number of characters per line
  27.       nWords;          // number of words in the text file
  28.  
  29.  
  30.   ifstream inFile; // handle for the input text file
  31.  
  32.   string fileName; // complete file name including the path
  33.  
  34.   fileName = getInputFileName(); // prompt and obtain the full file name
  35.  
  36.   inFile.open(fileName.c_str()); // try to open the file
  37.  
  38.   if( !inFile.is_open() )    // test for unsuccessfull file opening
  39.    {
  40.      cerr << "Cannot open file: " << fileName << endl << endl;
  41.      exit (0);
  42.    }
  43.  
  44.  
  45.   nChars = numCharsInFile( inFile, nLines ); // determine the number of lines
  46.                                             //    and characters in the file
  47.   nWords = numWordsInFile( inFile, nWords); // determine the number of words
  48.  
  49.   avgCharsPerLine = nChars / nLines;
  50.  
  51.  
  52.   cout << "The number of characters in the file: " << fileName
  53.        << " is = " << nChars << endl << endl;
  54.  
  55.   cout << "The number of lines in the file: " << fileName
  56.        << " is = " << nLines << endl << endl;
  57.  
  58.  
  59.   cout << "The number of Words in the file: " << fileName
  60.        << " is = " << nWords << endl << endl;
  61.  
  62.   cout << "The average number of characters per line in the text file: "
  63.        << fileName << " is: " << avgCharsPerLine << endl << endl;
  64.     cin>>c;
  65.   inFile.close(); // close the input file
  66.  
  67. }
  68.  
  69.  
  70.  
  71. string getInputFileName()
  72.  {
  73.    string fName; // fully qualified name of the file
  74.  
  75.    cout << "Please enter the fully qualified name of the " << endl
  76.         << "input text file (i.e. including the path): ";
  77.    cin >> fName; // cannot handle blanks in a file name or path
  78.    cout << endl; 
  79.  
  80.    return fName;
  81.  }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. int numCharsInFile( ifstream &in, int &numLines )
  88.  {
  89.    int numChars = 0; 
  90.  
  91.    char ch; // character holder;
  92.  
  93.    numLines = 0; // initialize the number of lines to zero
  94.  
  95.    while ( in.get(ch) ) // get the next character from the file
  96.                         //   the function get will also get whitespace
  97.                         //   i.e. blanks, tabs and end of line characters
  98.     {
  99.      if (ch != ' ' )
  100.      {
  101.        if(ch != '\n')
  102.        numChars++;// increase the count of characters by one if ch is NOT '\n' AND NOT a blank space
  103.        else
  104.        {
  105.        numLines++;     // increase the count of lines by one if ch IS '\n'
  106.        }
  107.      } 
  108.     }
  109.     numLines += 1; // for some reason it needs to add one and the results are correct
  110.    return numChars; 
  111.  }
  112.  
  113.  
  114.  
  115.  
  116. int numWordsInFile( ifstream &in, int &nWords)
  117.  {
  118.     in.clear();
  119.  
  120.     in.seekg(0, ios_base::beg); 
  121.  
  122.     int numWords = 0 ; 
  123.  
  124.    char ch; 
  125.  
  126.  
  127.    while (in.get(ch)) 
  128.    {      
  129.  
  130.     if ( ch == ' ' || ch == '\n' || ch == '\t' ) 
  131.        numWords++;    
  132.  
  133.  
  134.     }
  135.  
  136.    return numWords+1; 
  137.  }
Nov 5 '07 #1
Share this Question
Share on Google+
3 Replies


100+
P: 147
can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of characters, words, and lines but i need the occurance of each word.
I'm not going to write the code, but think about it: If you're going to count the occurrence of each distinct word, you'll need to remember those words. So as you read words in, you'll need to store them so that subsequent words can be compared to them. How you store them is up to you, as is how you compare them. STL containers can be a big help there. Take a look at std::set if you're not familiar with it; it's a container which can't hold duplicate elements, which lets you easily determine if a word occurs more than once in the file. Another good way to do this might be to just store each word as it is written, then sort the words in alphabetical order: recurring words will appear next to each other, making it easy to count them.
Nov 5 '07 #2

Expert 100+
P: 849
std::map would probably be better than std::set, since that way you can map strings (words) to integers (frequency counts).
Nov 5 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Also keep in mind that the >> operator stops on whitespace. You can fetch one word by:
Expand|Select|Wrap|Line Numbers
  1. sting str;
  2. fileName >> str;
  3.  
Also, you are not required to declare your variables at the beginning of each function. It looks like you have a C background and are just starting out on C++.
Nov 6 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.