fork download
  1. // Elaine Torrez CS1A
  2. // _____________________________________________________________
  3. //
  4. // CALCULATE AVERAGE LETTERS PER WORD
  5. // _____________________________________________________________
  6. // This program prompts the user to enter a sentence. It then
  7. // determines the number of words and the total number of letters
  8. // in the sentence. Finally, it calculates and displays the
  9. // average number of letters in each word.
  10. // _____________________________________________________________
  11. // INPUT
  12. // userStr : The sentence entered by the user
  13. //
  14. // OUTPUT
  15. // words : Number of words in the sentence
  16. // letters : Number of letters (A–Z / a–z)
  17. // average : Average number of letters per word
  18. // _____________________________________________________________
  19.  
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. // FUNCTION PROTOTYPES
  24. int countWords(const char *str);
  25. int countLetters(const char *str);
  26.  
  27. int main()
  28. {
  29. /****************************************************
  30.   * VARIABLE DECLARATIONS
  31.   ****************************************************/
  32. const int SIZE = 200;
  33. char userStr[SIZE]; // INPUT sentence
  34. int words; // Number of words
  35. int letters; // Number of letters
  36. float average; // Average letters per word
  37.  
  38. /****************************************************
  39.   * INPUT
  40.   ****************************************************/
  41. cout << "Enter a sentence: ";
  42. cin.getline(userStr, SIZE);
  43.  
  44. /****************************************************
  45.   * PROCESS
  46.   ****************************************************/
  47. words = countWords(userStr);
  48. letters = countLetters(userStr);
  49.  
  50. if (words > 0)
  51. average = static_cast<float>(letters) / words;
  52. else
  53. average = 0;
  54.  
  55. /****************************************************
  56.   * OUTPUT
  57.   ****************************************************/
  58. cout << "\nWords : " << words << endl;
  59. cout << "Letters : " << letters << endl;
  60. cout << "Average : " << average << endl;
  61.  
  62. return 0;
  63. }
  64.  
  65. // *************************************************************
  66. // countWords
  67. // -------------------------------------------------------------
  68. // Returns the number of words in a C-string. A word is counted
  69. // when transitioning from a space to a non-space character.
  70. // *************************************************************
  71. int countWords(const char *str)
  72. {
  73. int count = 0;
  74. bool inWord = false;
  75.  
  76. for (int i = 0; str[i] != '\0'; i++)
  77. {
  78. if (str[i] != ' ' && !inWord)
  79. {
  80. inWord = true;
  81. count++;
  82. }
  83. else if (str[i] == ' ')
  84. {
  85. inWord = false;
  86. }
  87. }
  88.  
  89. return count;
  90. }
  91.  
  92. // *************************************************************
  93. // countLetters
  94. // -------------------------------------------------------------
  95. // Returns the number of alphabetic letters (A–Z, a–z) in
  96. // a C-string. Spaces and punctuation are ignored.
  97. // *************************************************************
  98. int countLetters(const char *str)
  99. {
  100. int count = 0;
  101.  
  102. for (int i = 0; str[i] != '\0'; i++)
  103. {
  104. if ((str[i] >= 'A' && str[i] <= 'Z') ||
  105. (str[i] >= 'a' && str[i] <= 'z'))
  106. {
  107. count++;
  108. }
  109. }
  110.  
  111. return count;
  112. }
  113.  
Success #stdin #stdout 0.01s 5284KB
stdin
Four score and seven years ago
stdout
Enter a sentence: 
Words   : 6
Letters : 25
Average : 4.16667