fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <cctype>
  6.  
  7. int main()
  8. {
  9. std::string words;
  10. std::vector<std::string> dWords;
  11. while(std::cin >> words)
  12. {
  13. // remove punctuation
  14. words.erase(std::remove_if(words.begin(), words.end(), [](char ch)
  15. { return ::ispunct(static_cast<int>(ch)); }), words.end());
  16. dWords.push_back(words);
  17. }
  18.  
  19. // partition D from non-D words
  20. auto iter = std::partition(dWords.begin(), dWords.end(), [](const std::string& s)
  21. { return toupper(s[0]) == 'D'; });
  22.  
  23. // output results
  24. std::cout << "The number of words starting with D: " << std::distance(dWords.begin(), iter) << "\n";
  25. std::cout << "Here are the words:\n";
  26. std::copy(dWords.begin(), iter, std::ostream_iterator<std::string>(std::cout, " "));
  27.  
  28. std::cout << "\n\nThe number of words not starting with D: " << std::distance(iter, dWords.end()) << "\n";
  29. std::cout << "Here are the words:\n";
  30. std::copy(iter, dWords.end(), std::ostream_iterator<std::string>(std::cout, " "));
  31. }
  32.  
Success #stdin #stdout 0s 4956KB
stdin
This contains words that start definitely with the letter Dee. Do, Do not, Desert, and dumpling.
stdout
The number of words starting with D: 6
Here are the words:
dumpling Desert Do Do Dee definitely 

The number of words not starting with D: 10
Here are the words:
with the letter start that words not contains and This