fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7. cout.tie(nullptr);
  8.  
  9. string s;
  10. getline(cin,s);
  11. deque<char>dq;
  12.  
  13. for (int i = 0; i < s.size(); ++i){
  14. if(i != 0&& s[i] == ' ' && s[i - 1] != ' ' && s[i - 1] != ',' && s[i - 1] != '.' && s[i - 1] != '!' && s[i - 1] != '?'){
  15. dq.push_back(' ');
  16. }
  17. else if(s[i] >= 'a' && s[i] <= 'z'){
  18. dq.push_back(s[i]);
  19. }
  20. else if(s[i] == ',' || s[i] == '.' || s[i] == '!' || s[i] == '?') {
  21. if (!dq.empty() && dq.back()==' ') {
  22. dq.pop_back();
  23. }
  24. dq.push_back(s[i]);
  25. if (i + 1 < s.size() && s[i + 1] != ' ') {
  26. dq.push_back(' ');
  27. }
  28. }
  29. }
  30.  
  31. for (char c : dq) {
  32. cout << c;
  33. }
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty