fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4.  
  5. int count(std::string input)
  6. {
  7. static const std::set<char> delimeters{' ', '.', ',', ':', '-'};
  8. static const std::set<char>::iterator _end = delimeters.end();
  9.  
  10. int word_count{};
  11. int state{};
  12. for (std::string::iterator it=input.begin(); it!=input.end(); ++it)
  13. if (delimeters.find(*it) == _end) state++;
  14. else
  15. {
  16. word_count+= state > 0 ? (state+1)%2 : 0;
  17. state = 0;
  18. }
  19.  
  20. word_count+= state > 0 ? (state+1)%2 : 0;
  21.  
  22. return word_count;
  23. }
  24.  
  25.  
  26. int main() {
  27. // your code goes here
  28.  
  29. std::cout << count("- one, two,three . four :five");
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
2