So I'm studying this book, and I came across an exercise which (briefly) wants me to remove all white spaces of a char-array by using a function: void removeSpaces(char* s)
[iostream, cstring are included and SIZE is defined]
This is main():
int main() {
char a[SIZE] = "a bb ccc d";
cout << a << endl; // a bb ccc d
removeSpaces(a);
cout << a << endl; // a bb ccc d instead of abbcccd
}
This is removeSpaces():
void removeSpace(char* s) {
int size = strlen(s);
char* cpy = s; // an alias to iterate through s without moving s
char* temp = new char[size]; // this one produces the desired string
s = temp; // s points to the beginning of the desired string
while(*cpy) {
if(*cpy == ' ')
cpy++;
else
*temp++ = *cpy++;
}
cout << s << endl; // This prints out the desired result: abbcccd
}
(My choice of names isn't ideal, but nevermind that now.) So my function basically does what I want it to do, except that the result has no effect outside of the function's scope. How can I accomplish that? What am I missing, what am I doing wrong?