Skip to main content
Added spaces to removeSpaces function.
Source Link
AustinWBryan
  • 3.3k
  • 3
  • 28
  • 44
#include<bits/stdc++.h> using namespace std ;std;  void removeSpaces(string &str) {   int n = str.length();   int i = 0, j = -1; bool spaceFound = false;   while (++j <= n && str[j] == ' '); while (j <= n) { if (str[j] != ' ') { if ((str[j] == '.' || str[j] == ',' || str[j] == '?') && i - 1 >= 0 && str[i - 1] == ' ') str[i - 1] = str[j++];   else  str[i++] = str[j++];     spaceFound = false; } else if (str[j++] == ' ') {     if (!spaceFound) { str[i++] = ' '; spaceFound = true; } } } if (i <= 1)   str.erase(str.begin() + i, str.end()); else  str.erase(str.begin() + i - 1, str.end()); } int main() { string s; cin>>s;cin >> s;  for(int i=si = s.find("WUB");i>=0;i=s; i >= 0; i = s.find("WUB")) { s.replace(i,3," "); } removeSpaces(s); cout<<s<<endl;cout << s << endl; return 0; } 
#include<bits/stdc++.h> using namespace std ; void removeSpaces(string &str) {   int n = str.length();   int i = 0, j = -1; bool spaceFound = false;   while (++j <= n && str[j] == ' '); while (j <= n) { if (str[j] != ' ') { if ((str[j] == '.' || str[j] == ',' || str[j] == '?') && i - 1 >= 0 && str[i - 1] == ' ') str[i - 1] = str[j++];   else  str[i++] = str[j++];     spaceFound = false; } else if (str[j++] == ' ') {     if (!spaceFound) { str[i++] = ' '; spaceFound = true; } } } if (i <= 1) str.erase(str.begin() + i, str.end()); else  str.erase(str.begin() + i - 1, str.end()); } int main() { string s; cin>>s; for(int i=s.find("WUB");i>=0;i=s.find("WUB")) { s.replace(i,3," "); } removeSpaces(s); cout<<s<<endl; return 0; } 
#include<bits/stdc++.h> using namespace std;  void removeSpaces(string &str) { int n = str.length(); int i = 0, j = -1; bool spaceFound = false; while (++j <= n && str[j] == ' '); while (j <= n) { if (str[j] != ' ') { if ((str[j] == '.' || str[j] == ',' || str[j] == '?') && i - 1 >= 0 && str[i - 1] == ' ') str[i - 1] = str[j++]; else str[i++] = str[j++]; spaceFound = false; } else if (str[j++] == ' ') { if (!spaceFound) { str[i++] = ' '; spaceFound = true; } } } if (i <= 1)   str.erase(str.begin() + i, str.end()); else str.erase(str.begin() + i - 1, str.end()); } int main() { string s; cin >> s;  for(int i = s.find("WUB"); i >= 0; i = s.find("WUB")) s.replace(i,3," "); removeSpaces(s); cout << s << endl; return 0; } 
Source Link
mubasshir00
  • 349
  • 3
  • 10

You can use this code for remove subtring and also replace , and also remove extra white space . code :

#include<bits/stdc++.h> using namespace std ; void removeSpaces(string &str) { int n = str.length(); int i = 0, j = -1; bool spaceFound = false; while (++j <= n && str[j] == ' '); while (j <= n) { if (str[j] != ' ') { if ((str[j] == '.' || str[j] == ',' || str[j] == '?') && i - 1 >= 0 && str[i - 1] == ' ') str[i - 1] = str[j++]; else str[i++] = str[j++]; spaceFound = false; } else if (str[j++] == ' ') { if (!spaceFound) { str[i++] = ' '; spaceFound = true; } } } if (i <= 1) str.erase(str.begin() + i, str.end()); else str.erase(str.begin() + i - 1, str.end()); } int main() { string s; cin>>s; for(int i=s.find("WUB");i>=0;i=s.find("WUB")) { s.replace(i,3," "); } removeSpaces(s); cout<<s<<endl; return 0; }