Tuesday, April 11, 2017

Programming Challenge 10.3 - Word Counter

/* Word Counter - This program contains a function that accepts a
   pointer to a C-string as an argument, that returns the number
   of words contained in the string. For instance, if the string
   argument is "Four score and seven years ago" the function returns
   the number 6.
  
   The program asks the user to input a string and then passes it to
   the function. The number of words in the string is displayed on
   the screen.
  
   Optional Exercise: This program also contains an overloaded version
   of this function that accepts a string class object as its argument. */

#include "Utility.h"

/* Counts the words contained in the string, returns the number of words*/
int countWords(char *);

/* Overloaded function: Counts words in the string class object, returns
the number of words */
int countWords(string);

int main()
{
   const int NUM_WORDS = 501;
   int         numWords = 0;
   char         sentence[NUM_WORDS];
   string     ctrlSentence = " ";

   cout << "\n\t\tWORD COUNT\n\n"
        << "\tEnter a sentence, " << (NUM_WORDS - 1)
        << " characters in length:\n\t";
   cin.getline(sentence, NUM_WORDS);

   cout << "\n\tYour sentence contains: " << (numWords = countWords(sentence))
      << " words.\n";

   cout << "\n\tEnter another sentence:\n\t";
   getline(cin, ctrlSentence);

   cout << "\n\tThis sentence contains: " << (numWords = countWords(ctrlSentence))
      << " words.\n";

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: countWords

   This function accepts a pointer to a C-string as argument.
   It counts the words contained in the sentence passed to
   the function and returns this number.
   ********************************************************** */

int countWords(char *wordPtr)
{
   int  wordCount = 0;
   char delimList[] = "\" ´-.()*=_!?~<<>>:,\t\n";
   char *next_word = NULL;

   wordPtr = strtok_s(wordPtr, delimList, &next_word);

   while (wordPtr != NULL)
   {
      if (wordPtr != NULL)
      {
         wordPtr = strtok_s(NULL, delimList, &next_word);

         ++wordCount;
      }
   }

   return wordCount;
}

/* **********************************************************
   Definition: countWords

   This overloaded version of the function countWords accepts
   a string class object as argument. It counts the words in
   the string and returns the count.
   ********************************************************** */

int countWords(string ctrlString)
{
   size_t wordCount = 0,
          numDelims = 0;

   bool isDelim = true;

   for (size_t index = 0; index < ctrlString.length(); ++index)
   {
      /* If a delimiter or whitespace is found, numDelim increments,
         and isDelim(iter) gets true. Else a word is found, and
         wordCount increments. */
      if (isspace(ctrlString[index]) || ispunct(ctrlString[index]))
      {
        numDelims++;
           isDelim = true;
      }
      else if (isDelim && !ispunct(ctrlString[index + 1]))
      {
         isDelim = false;
         ++wordCount;
      }
   }

   return wordCount;
}

Example Output:






1 comment: