// Andres Guzman CSC5 Chapter 10, P. 589, #4
/******************************************************************************
*
* DISPLAY ESSAY WORD COUNT #2
* ____________________________________________________________________________
* Program function will accept pointer to a c-string as an argument and will
* return the number of words typed, and afterwards will also determine the
* average character count amongst the words.
*
* Computation:
*
* Function will compute using 2 if statements nested inside a for loop along
* with an addition if statement outside.
* ____________________________________________________________________________
*
* INPUT
*
* SIZE = Array size of characters
* sentence = Holds entered characters
*
* OUTPUT
*
* wordcount = total words counted
* average = the average amount of characters used per word
*
******************************************************************************/
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
int words(char *a); //Function Prototype
double average(char *a); //Function Prototype
int main()
{
const int SIZE = 1000; //Array Size
char sentence[SIZE]; //Array
cout << "Enter a Sentence: ";
cin.getline(sentence, SIZE); //Allows entire entered line with SIZE limit
//
//Function call inside console output for word count
cout << "\nThere are " << words(sentence)
<< " Words in this Sentence." << endl;
//
//Function call inside of console output for average
cout << fixed << setprecision(2);
cout << "This sentence has an average of " << average(sentence)
<< " Letters per word.";
return 0;
}
/*******************************************************************************
* Adding Definition to function. This function will step through each character
* of the array and determine whether if it isn't a whitespace character or is a
* null terminator. If it isn't a whitespace character, it will check whether it
* is the beginning of the subscript, or if the prior element of the subscript
* was a whitespace character.
******************************************************************************/
int words(char *a)
{
int wordcount = 0; //Accumulator
//
//Initializing for loop to step through subscript array
for (int count = 0; a[count] != '\0'; count++)
{
//
//If both conditions are true, then a word is counted
if(!isspace(a[count]) && (count == 0 || isspace(a[count - 1])))
wordcount++; //Increments if both conditions are true
}
return wordcount;
}
/*******************************************************************************
* Adding Definition to function. This function will follow the same sequence as
* the words function, with the addition of calculating the average characters
* used per word by only counting alphabetical characters.
******************************************************************************/
double average(char *a)
{
int wordcount = 0; //Accumulator
int wordlength = 0; //Accumulator
double average; //Variable to hold calculations
//
//Initializing For loop to step through array subscript
for (int count = 0; a[count] != '\0'; count++)
{
//
//If both conditions are true, then a word is counted
if(!isspace(a[count]) && (count == 0 || isspace(a[count - 1])))
wordcount++;
//
//If condition is true, increments the variable by one as a character
if(isalpha(a[count]))
wordlength++;
}
//
//If statement is true, then 0 is returned to avoid garbage data
if(wordcount == 0)
return 0;
//
//Calculation formula
average = static_cast<double>(wordlength) / wordcount;
return average; //Returns average value
}