fork download
  1. //(с)Terminator
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. //1-ый способ
  8. void space_unique(string& s){
  9. string::size_type p = 0;
  10. while((p = s.find(' ', p)) != string::npos){
  11. if((p + 1 < s.length()) && (s[p + 1] == ' '))
  12. s.erase(p, 1);
  13. else
  14. ++p;
  15. }
  16. }
  17.  
  18.  
  19.  
  20. //2-ой способ
  21. void space_unique(string& d, const string& s){
  22. string::size_type f, l;
  23.  
  24. for(l = f = 0; (l = s.find(' ', l)) != string::npos; l = f){
  25. d.append(s.begin() + f, s.begin() + (l + 1));
  26. f = s.find_first_not_of(' ', l);
  27. }
  28. if(f < s.length())
  29. d.append(s.begin() + f, s.end());
  30. }
  31.  
  32.  
  33.  
  34.  
  35. int main(void){
  36.  
  37. string str = "\1 TASM MASM FASM";
  38.  
  39. space_unique(str);
  40. cout << str << endl;
  41.  
  42. //...
  43.  
  44. string res;
  45. str = " Apple Orange Potate . ops alien fin";
  46. space_unique(res, str);
  47. cout << res;
  48. return 0;
  49. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
 TASM MASM FASM
 Apple Orange Potate . ops alien fin