fork(47) download
  1. #include <iostream>
  2. #include <string> // forget about char[] and char*: use string instead !
  3. #include <algorithm> // for copy_if()
  4. #include <iterator> // for the back_inserter
  5. using namespace std;
  6.  
  7. void remove_extra_whitespaces(const string &input, string &output)
  8. {
  9. output.clear(); // unless you want to add at the end of existing sring...
  10. unique_copy (input.begin(), input.end(), back_insert_iterator<string>(output), [](char a,char b){ return isspace(a) && isspace(b);});
  11. cout << output<<endl;
  12. }
  13.  
  14. int main(int argc, char **argv)
  15. {
  16. cout << "testing 2 ..\n";
  17.  
  18. string input = "asfa sas f f dgdgd dg ggg";
  19. string output = "NO_OUTPUT_YET";
  20. remove_extra_whitespaces(input,output);
  21.  
  22. return 0;
  23. }
  24.  
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
testing 2 ..
asfa sas f f dgdgd dg ggg