How to make it work faster? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to make it work faster?

This code erases unnecessary gap between the symbols. For example : Input: abc de f . Output: abc de f . #include <iostream> #include <string> using namespace std; string delUnnecessary (string &str) { int size = str.length(); for(int j = 0; j<=size; j++) { for(int i = 0; i <=j; i++) { if(str[i] == ' ' && str[i+1] == ' ') { str.erase(str.begin() + i); } else if(str[0]== ' ') { str.erase(str.begin()); } else if(str[i] == '\0' && str[i-1]== ' ') { str.erase(str.end() - 1); } } } return str; } int main(){ ios_base::sync_with_stdio(0); freopen("trim.in","r",stdin); freopen("trim.out","w",stdout); string s; getline(cin,s); cout«delUnnecessary(s); return 0; }

27th Apr 2019, 10:32 AM
Val
Val - avatar
2 Answers
+ 2
Here you can read the answer. Its to much to write it all down here ;) https://www.google.com/amp/s/www.techiedelight.com/remove-whitespaces-string-cpp/amp/
27th Apr 2019, 10:34 AM
Dragonxiv
Dragonxiv - avatar
0
Dragonxiv , thank you!
27th Apr 2019, 10:36 AM
Val
Val - avatar