ahmed0saber

Remove additional spaces from a text in C++

May 31st, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6.     string _text = "My  name    is        Ahmed    Saber" , mod_text = "";
  7.     bool space = true;
  8.     for(int i=0;i<_text.length();i++)
  9.     {
  10.         if(_text[i]==' ' && space)
  11.         {
  12.             space=false;
  13.             mod_text+=' ';
  14.         }
  15.         else if(_text[i]==' ' && !space)
  16.         { }
  17.         else
  18.         {
  19.             mod_text+=_text[i];
  20.             space=true;
  21.         }
  22.     }
  23.     cout<<"Before : "<<_text<<endl;
  24.     cout<<"After : "<<mod_text;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment