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; }