fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <regex>
  4. #include <iterator>
  5.  
  6. std::string fix_string(const std::string& str) {
  7. static const std::regex rgx_pattern("\\s+(?=[\\.,])");
  8. std::string rtn;
  9. rtn.reserve(str.size());
  10. std::regex_replace(std::back_insert_iterator<std::string>(rtn),
  11. str.cbegin(),
  12. str.cend(),
  13. rgx_pattern,
  14. "");
  15. return rtn;
  16. }
  17.  
  18. int main(int argc, char *argv[]) {
  19. std::string str = "This , is a string . And another .";
  20. std::string new_str = fix_string(str);
  21.  
  22. std::cout << "Old String: " << str << "\n";
  23. std::cout << "Fixed String: " << new_str << std::endl;
  24.  
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 3560KB
stdin
Standard input is empty
stdout
Old String: This  , is a string   . And another .
Fixed String: This, is a string. And another.