fork download
  1. //ANDREW ALSPAUGH CS1A Chapter 10. P. 588 #3
  2. //
  3. /****************************************************************************
  4. Calculate Total Words
  5. ____________________________________________________________________________
  6. This program displays a user inputted string (less than 100 characters)
  7. and also displays the total amount of words in the string.
  8. ____________________________________________________________________________
  9. //Input
  10. const int SIZE = 100;
  11. char string[SIZE];
  12. char space = ' ';
  13.  
  14. //Output
  15. int spaceCount = 0;
  16. int wordCount = 0;
  17.  
  18. ****************************************************************************/
  19. #include <iostream>
  20. #include <cstring>
  21. using namespace std;
  22.  
  23. //WordCount Prototype
  24. int GetWordCount (char *string, const int SIZE, char &space, int &spaceCount, int &wordCount);
  25.  
  26. int main()
  27. {
  28. //DATA DICTIONARY
  29. //Input
  30. const int SIZE = 100;
  31. char string[SIZE];
  32. char space = ' ';
  33.  
  34. //Output
  35. int spaceCount = 0;
  36. int wordCount = 0;
  37.  
  38. //INPUT
  39. cin.getline (string, SIZE);
  40.  
  41. //PROCESS
  42. wordCount = GetWordCount( string, SIZE, space, spaceCount, wordCount);
  43.  
  44. //OUTPUT
  45. cout << "User entered String:" << endl;
  46. cout << string << endl << endl;
  47.  
  48. cout << "There are " << wordCount << " words" << endl;
  49.  
  50. return 0;
  51. }
  52.  
  53. //WordCount Definition
  54. int GetWordCount (char *string, const int SIZE, char &space, int &spaceCount, int &wordCount)
  55. {
  56. for (int count = 0; string[count] != '\0' ; count++)
  57. {
  58. if (*(string + count) == space)
  59. spaceCount += 1;
  60. }
  61.  
  62. wordCount = spaceCount + 1;
  63.  
  64. return wordCount;
  65. }
  66.  
Success #stdin #stdout 0.01s 5288KB
stdin
A small breeze carried the scent of pine through the quiet valley
stdout
User entered String:
A small breeze carried the scent of pine through the quiet valley

There are 12 words