fork download
  1. //Natalie Zarate CSC 5 Chapter 10, P. 589, #4
  2. /*******************************************************************************
  3.  * EVALUATE PHRASE
  4.  *
  5.  * This program will prompt the user for a phrase within 40 characters. With
  6.  * this phrase, the program will find and display the word count and average
  7.  * word length of the phrase.
  8.  *
  9.  * INPUT
  10.  * phrase - user inputted phrase
  11.  *
  12.  * OUPUT
  13.  * wordCount - given phrase's word count
  14.  * wordLengthAVg - given phrase's average word length
  15.  * ****************************************************************************/
  16. #include <string>
  17. #include <iostream>
  18. #include <cctype>
  19. using namespace std;
  20.  
  21. /*******************************************************************************
  22.  * countWords
  23.  * Counts the number of words in a given phrase
  24.  * _____________________________________________________________________________
  25.  * INPUT
  26.  * string - Pointer to the phrase
  27.  * letter - flag; indicates whether character is a letter or space
  28.  *
  29.  * OUTPUT
  30.  * words - Number of words in the given phrase.
  31.  * ****************************************************************************/
  32. // Prototype for countWords
  33. int countWords(const char *string);
  34.  
  35. /*******************************************************************************
  36.  * avgWordL
  37.  * This funtion calculates the average word length of the given phrase
  38.  * _____________________________________________________________________________
  39.  * INPUT
  40.  * string - pointer to given phrase
  41.  * words - phrase word count
  42.  * OUTPUT
  43.  * average - average word length
  44.  * ****************************************************************************/
  45. // Prototype fot avgWordL
  46. float avgWordL(const char *string, int words);
  47.  
  48. int main()
  49. {
  50. char phrase[41]; // INPUT - stores given phrase
  51. int wordCount; // OUTPUT - word count of phrase
  52. float wordLengthAvg; // OUTPUT - average word length
  53.  
  54. // Prompt user for phrase
  55. cout << "Enter Phrase (40 Character Limit):" << endl;
  56.  
  57. // Get length of phrase
  58. cin.getline(phrase,sizeof(phrase));
  59.  
  60. // Call countWords
  61. wordCount = countWords(phrase);
  62.  
  63. // Display wordCount
  64. cout << "WORD COUNT: " << wordCount << endl;
  65.  
  66. // Call avgWordL
  67. wordLengthAvg = avgWordL(phrase, wordCount);
  68.  
  69. // Display average owrd length
  70. cout << "AVERAGE WORD LENGTH: " << wordLengthAvg << endl;
  71.  
  72. return 0;
  73. }
  74.  
  75. int countWords(const char *string)
  76. {
  77. int words = 0;
  78. bool letter = false; // INPUT - flag; dictates whether current character is a
  79. // letter or space
  80.  
  81. //Step through array
  82. while (*string)
  83. {
  84. // Check if character is a string
  85. if (isspace(*string))
  86. {
  87. //Character is a space/ not a letter
  88. letter = false;
  89. }
  90. else if (!letter)
  91. {
  92. //Character is a letter
  93. letter = true;
  94.  
  95. // Increment number of words
  96. words++;
  97. }
  98.  
  99. // Move to next element
  100. string++;
  101. }
  102.  
  103. return words;
  104. }
  105.  
  106. float avgWordL(const char *string, int words)
  107. {
  108. int wordLength; // INPUT - length of word in given phrase
  109. bool letter; // Flag; indicates whether a character is a letter or space
  110. float average; // OUPUT - average word length in phrase
  111.  
  112. //Step through array
  113. while(*string)
  114. {
  115. // Check if character is a space
  116. if(isspace(*string))
  117. {
  118. // Character is a space
  119. letter = false;
  120. }
  121. else
  122. {
  123. letter = true;
  124.  
  125. // Increment word length
  126. wordLength++;
  127. }
  128.  
  129. string++;
  130. }
  131.  
  132. // Coumpute average word length
  133. average = wordLength / words;
  134.  
  135. return average;
  136.  
  137.  
  138. }
Success #stdin #stdout 0.01s 5280KB
stdin
I have outlived the night
stdout
Enter Phrase (40 Character Limit):
WORD COUNT: 5
AVERAGE WORD LENGTH: 5