fork download
  1. //Zachary Abdollahi CS1A Chapter 10, P. 589, #4
  2. /*******************************************************************************
  3. *
  4. * COMPUTE AVERAGE LETTERS PER WORD
  5. *_______________________________________________________________________________
  6. * This program accepts a C-string from the user, passes a
  7. * pointer to the C-string to functions that count the total
  8. * number of words and alphabetic letters, and displays the
  9. * word count along with the average number of letters per word.
  10. *_______________________________________________________________________________
  11. * INPUT
  12. * userString : C-string entered by the user
  13. *
  14. * OUTPUT
  15. * wordCount : Number of words contained in the C-string
  16. * averageLetters: Average number of letters per word
  17. *
  18. *******************************************************************************/
  19.  
  20. #include <iostream>
  21. #include <iomanip>
  22. #include <cctype>
  23. using namespace std;
  24.  
  25. // Function Prototypes
  26. int countWords(const char *strPtr);
  27. int countLetters(const char *strPtr);
  28.  
  29. int main ()
  30. {
  31. const int SIZE = 200; // Maximum C-string size
  32. char userString[SIZE]; //INPUT
  33. int wordCount; //OUTPUT
  34. int letterCount; // Total alphabetic letters in string
  35. double averageLetters = 0.0; //OUTPUT
  36.  
  37. // Input String
  38. cout << "Enter a string: ";
  39. cin.getline(userString, SIZE);
  40.  
  41. // Compute Word Count and Average Letters per Word
  42. wordCount = countWords(userString);
  43. letterCount = countLetters(userString);
  44.  
  45. if (wordCount > 0)
  46. {
  47. averageLetters = static_cast<double>(letterCount) / wordCount;
  48. }
  49.  
  50. // Output Results
  51. cout << fixed << setprecision(1);
  52. cout << "The string contains " << wordCount << " words." << endl;
  53. cout << "The average number of letters per word is "
  54. << averageLetters << "." << endl;
  55.  
  56. return 0;
  57. }
  58.  
  59. /*******************************************************************************
  60. *
  61. * FUNCTION countWords
  62. *_______________________________________________________________________________
  63. * This function accepts a pointer to a C-string, counts the
  64. * number of words separated by whitespace characters, and
  65. * returns the total word count as an integer.
  66. *_______________________________________________________________________________
  67. * INPUT
  68. * strPtr : Pointer to a null-terminated C-string
  69. *
  70. * OUTPUT
  71. * words : Integer count of words in the C-string
  72. *
  73. *******************************************************************************/
  74. int countWords(const char *strPtr)
  75. {
  76. int words = 0; //OUTPUT
  77. bool inWord = false; // Flag to track if currently inside a word
  78.  
  79. while (*strPtr != '\0')
  80. {
  81. if (isspace(*strPtr))
  82. {
  83. inWord = false;
  84. }
  85. else if (!inWord)
  86. {
  87. inWord = true;
  88. words++;
  89. }
  90.  
  91. strPtr++;
  92. }
  93.  
  94. return words;
  95. }
  96.  
  97. /*******************************************************************************
  98. *
  99. * FUNCTION countLetters
  100. *_______________________________________________________________________________
  101. * This function accepts a pointer to a C-string, counts the
  102. * number of alphabetic letters in the string, and returns the
  103. * total letter count as an integer.
  104. *_______________________________________________________________________________
  105. * INPUT
  106. * strPtr : Pointer to a null-terminated C-string
  107. *
  108. * OUTPUT
  109. * letters : Integer count of alphabetic letters
  110. *
  111. *******************************************************************************/
  112. int countLetters(const char *strPtr)
  113. {
  114. int letters = 0; //OUTPUT
  115.  
  116. while (*strPtr != '\0')
  117. {
  118. if (isalpha(*strPtr))
  119. {
  120. letters++;
  121. }
  122.  
  123. strPtr++;
  124. }
  125.  
  126. return letters;
  127. }
Success #stdin #stdout 0s 576KB
stdin
Four score and seven years ago
stdout
Enter a string: The string contains 6 words.
The average number of letters per word is 4.2.