fork download
  1. // Andres Guzman CSC5 Chapter 10, P. 589, #4
  2. /******************************************************************************
  3.  *
  4.  * DISPLAY ESSAY WORD COUNT #2
  5.  * ____________________________________________________________________________
  6.  * Program function will accept pointer to a c-string as an argument and will
  7.  * return the number of words typed, and afterwards will also determine the
  8.  * average character count amongst the words.
  9.  *
  10.  * Computation:
  11.  *
  12.  * Function will compute using 2 if statements nested inside a for loop along
  13.  * with an addition if statement outside.
  14.  * ____________________________________________________________________________
  15.  *
  16.  * INPUT
  17.  *
  18.  * SIZE = Array size of characters
  19.  * sentence = Holds entered characters
  20.  *
  21.  * OUTPUT
  22.  *
  23.  * wordcount = total words counted
  24.  * average = the average amount of characters used per word
  25.  *
  26.  ******************************************************************************/
  27. #include <iostream>
  28. #include <cctype>
  29. #include <iomanip>
  30. using namespace std;
  31.  
  32. int words(char *a); //Function Prototype
  33. double average(char *a); //Function Prototype
  34.  
  35. int main()
  36. {
  37. const int SIZE = 1000; //Array Size
  38. char sentence[SIZE]; //Array
  39. cout << "Enter a Sentence: ";
  40. cin.getline(sentence, SIZE); //Allows entire entered line with SIZE limit
  41. //
  42. //Function call inside console output for word count
  43. cout << "\nThere are " << words(sentence)
  44. << " Words in this Sentence." << endl;
  45. //
  46. //Function call inside of console output for average
  47. cout << fixed << setprecision(2);
  48. cout << "This sentence has an average of " << average(sentence)
  49. << " Letters per word.";
  50. return 0;
  51. }
  52. /*******************************************************************************
  53.  * Adding Definition to function. This function will step through each character
  54.  * of the array and determine whether if it isn't a whitespace character or is a
  55.  * null terminator. If it isn't a whitespace character, it will check whether it
  56.  * is the beginning of the subscript, or if the prior element of the subscript
  57.  * was a whitespace character.
  58.  ******************************************************************************/
  59. int words(char *a)
  60. {
  61. int wordcount = 0; //Accumulator
  62. //
  63. //Initializing for loop to step through subscript array
  64. for (int count = 0; a[count] != '\0'; count++)
  65. {
  66. //
  67. //If both conditions are true, then a word is counted
  68. if(!isspace(a[count]) && (count == 0 || isspace(a[count - 1])))
  69. wordcount++; //Increments if both conditions are true
  70. }
  71. return wordcount;
  72. }
  73. /*******************************************************************************
  74.  * Adding Definition to function. This function will follow the same sequence as
  75.  * the words function, with the addition of calculating the average characters
  76.  * used per word by only counting alphabetical characters.
  77.  ******************************************************************************/
  78. double average(char *a)
  79. {
  80. int wordcount = 0; //Accumulator
  81. int wordlength = 0; //Accumulator
  82. double average; //Variable to hold calculations
  83. //
  84. //Initializing For loop to step through array subscript
  85. for (int count = 0; a[count] != '\0'; count++)
  86. {
  87. //
  88. //If both conditions are true, then a word is counted
  89. if(!isspace(a[count]) && (count == 0 || isspace(a[count - 1])))
  90. wordcount++;
  91. //
  92. //If condition is true, increments the variable by one as a character
  93. if(isalpha(a[count]))
  94. wordlength++;
  95. }
  96. //
  97. //If statement is true, then 0 is returned to avoid garbage data
  98. if(wordcount == 0)
  99. return 0;
  100. //
  101. //Calculation formula
  102. average = static_cast<double>(wordlength) / wordcount;
  103. return average; //Returns average value
  104. }
Success #stdin #stdout 0s 544KB
stdin
Hello world, I am tired
stdout
Enter a Sentence: 
There are 5 Words in this Sentence.
This sentence has an average of 3.60 Letters per word.