This is the code that I've written to remove spaces in the given string. It normally works.
string a = "hello there world";
int n = a.size();
for(int i =0 ;i<n;++i){
if(a[i] == ' '){
a.erase(a.begin()+i);
}
}
cout<<a;
But for the given input above ( note that there are two spaces between "hello" and "there" ), my output is:
hello thereworld
"hello" and "world" still have a single space between them.
Is this behaviour normal? Is it something to do with erase
?