Advertisement
janac

Change double spaces in text to single spaces

Jan 3rd, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. // Change double spaces in text to single spaces.
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <regex>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main()
  11. {
  12.     string text("All  double  spaces  in  this text  should be single spaces.");
  13.     regex two_spaces("  "); // This is double space, the pattern we want to fix.
  14.  
  15.     cout << text << '\n'; // Show text to fix.
  16.     cout << "\nFixing double spaces ...\n";
  17.  
  18.     // Replace all double spaces with a single space.
  19.     string better_text = regex_replace(text, two_spaces, " ");
  20.  
  21.     cout << "\nText is fixed:\n";
  22.     cout << better_text << '\n';
  23.  
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement