fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. int GetWordCount (char *string, const int SIZE, char &space, int &spaceCount, int &wordCount);
  6.  
  7. int main()
  8. {
  9. const int SIZE = 100;
  10. char string[SIZE];
  11. char space = ' ';
  12.  
  13. int spaceCount = 0;
  14. int wordCount = 0;
  15.  
  16. cin.getline (string, SIZE);
  17.  
  18. wordCount = GetWordCount( string, SIZE, space, spaceCount, wordCount);
  19.  
  20. cout << "User entered String:" << endl;
  21. cout << string << endl << endl;
  22.  
  23. cout << "There are " << wordCount << " words" << endl;
  24.  
  25. return 0;
  26. }
  27.  
  28. int GetWordCount (char *string, const int SIZE, char &space, int &spaceCount, int &wordCount)
  29. {
  30. for (int count = 0; string[count] != '\0' ; count++)
  31. {
  32. if (*(string + count) == space)
  33. spaceCount += 1;
  34. }
  35.  
  36. wordCount = spaceCount + 1;
  37.  
  38. return wordCount;
  39. }
  40.  
Success #stdin #stdout 0s 5320KB
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