I tried to write a function that get a string with spaces and returns the string without the spaces.
for example:
str = " a f ";
will be replaced to "af";
my function doesn't work, it replaced the string to: "af f".
this is my function:
void remove_space(string& str) {
int len = str.length();
int j = 0, i = 0;
while (i < len) {
while (str.at(i) == ' ') i++;
str.at(j) = str.at(i);
i++;
j++;
}
}
int main ()
{
string str;
getline(cin, str);
remove_space(str);
cout << str << endl;
return 0;
}
any help appreciated!