Thursday, April 13, 2017

Programming Challenge 10.4 - Average Number Of Letters

/* Average Number Of Letters - This program is a modification of
   Programming Challenge 10.3 It displays the average number of
   letters in each word. */

#include "Utility.h"

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

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

int main()
{
   const int NUM_WORDS = 501;
   int         numWords = 0;
   double     letterCount = 0.0,
             ctrlLetters = 0.0,
             average = 0.0;
   char         sentence[NUM_WORDS];
   string     ctrlSentence = " ";

   cout << "\n\tWORD COUNT - AVERAGE NUMBER OF LETTERS\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, letterCount))
        << " words.";

   cout << fixed << setprecision(2);
   cout << "\n\tThe average number of characters is: "
      << (average = letterCount / numWords - 1) << "\n";

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

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

   cout << "\n\tThe average number of characters is: "
        << (average = ctrlLetters / numWords);

   pauseSystem();
   return 0;
}

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

   This function accepts a pointer to a C-string as argument.
   It determines the number of letters and stores this value
   in letterCount. It also counts the words contained in the
   sentence passed to the function and returns this number.
   ********************************************************** */

int countWords(char *wordPtr, double &letterCount)
{
   int            wordCount = 0;
   char            delimList[] = "\" ´-.()*=_!?~<<>>:,\t\n";
   char           *next_word = NULL;
   double        avg = 0.0;
   unsigned int count = 0;
  
   /* Get the letter count */
   while (count != strlen(wordPtr + 1))
   {
      if (!isascii(*wordPtr) && *delimList)
      {
         ++count;
      }
      else
      {
         ++letterCount;
      }
      count++;
   }

   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 function accepts a string class object as
   argument. It counts the letters, the value is stored in
   letterCount. It also counts the number of words, which
   number is returned from the function.
   ********************************************************** */

int countWords(string ctrlString, double &letterCount)
{
   size_t wordCount = 0,
          numDelims = 0,
          count = 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;
      }
   }

   /* Counts the number of letters */
   while (count < ctrlString.length())
   {
      if (isalpha(ctrlString[count]))
         ++letterCount;

      count++;
   }

   return wordCount;
}

Example Output:




No comments:

Post a Comment