简体   繁体   中英

Remove spaces from char-array by using a function with a pointer argument

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?

Your function shouldn't even do any string copying. It should just do an in-place replacement:

void removeSpace(char* s)
{
    for (char* s2 = s; *s2; ++s2) {
        if (*s2 != ' ')
            *s++ = *s2;
    }
    *s = 0;
}

Even terser:

void removeSpace(char* s)
{
    char* s2 = s;
    do {
        if (*s2 != ' ')
            *s++ = *s2;
    } while (*s2++);
}

Since you pass the pointer by value, you are surely changing the array in place. Obviously, you'd remove spaces like this:

void removeSpaces(char* s) {
    *std::remove(s, s + strlen(s), ' ') = 0;
}

You change the value of s but that is a local variable and thus has no affect outside the function:

void removeSpace(char* s)

You could change this to:

void removeSpace(char*& s)

This makes a reference to the passed value. Thus changing s will change the original. Unfortunately that does not work for you because of the way you call removeSpace() (as you pass an array).

char a[SIZE] = "a bb ccc d";
removeSpaces(a);

You could change your code too:

char buffer[SIZE] = "a bb ccc d";
char* a = buffer;
removeSpaces(a);

Now the modifications suggested above would work correctly. But you are leaking memory (you dynamically allocate memory in removeSpace() that is never released. The best way to resolve that is to use modern C++ techniques rather than write C and and compile it with the C++ compiler.

Couple of solutions:

  1. Modify the array in place (no dynamic allocation)
  2. Use proper containers (std::string or std::vector)
  3. Use the standard algorithms.

Just add one last line to your function:-

strcpy(s, temp);

and remove unnecessary ones:-

s = temp;

The call to removeSpaces has no affect because you are creating a temporary buffer in the function and copying the string into that while applying the transformation. You can fix this by removing the temporary buffer and just modifying the string in place.

void removeSpaces(char* s)
{
    char* cpy = s;  // an alias to iterate through s without moving s
    char* temp = s;

    while (*cpy)
    {
        if (*cpy != ' ')
            *temp++ = *cpy;
        cpy++;
    }
    *temp = 0;

    cout << s << endl; // This prints out the desired result: abbcccd
}

Your function receives a copy of the pointer a. Changing the copy within the function does not change the caller's original a pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM