fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. int countWords(const std::string& line)
  6. {
  7. std::istringstream str(line);
  8. std::string dump;
  9. int count = 0;
  10. while(str >> dump)
  11. ++count;
  12. return count;
  13. }
  14.  
  15. int main()
  16. {
  17. std::cout << "Enter line\n";
  18. std::string line;
  19. std::getline(std::cin, line);
  20. std::cout << "Sentence: " << line << '\n' <<
  21. "Word count: " << countWords(line) << '\n';
  22. }
  23.  
Success #stdin #stdout 0s 3436KB
stdin
a    quick brown  fox jumps over the lazy    dog
stdout
Enter line
Sentence: a    quick brown  fox jumps over the lazy    dog
Word count: 9