C code to remove spaces from a string

February 13, 2010 in Uncategorized

Write a C function to remove spaces from a string. The function header should be
void removeSpaces(char *str)

ie, “abc de” -> “abcde”

Here might be a common interview question testing your C skills:

It can be implemented in just 5 lines of code, shown below:

Of course, this is for illustration purpose and to show how elegant C code can be.

I probably wouldn’t program like this in the industry just for clarity sake. There are no parenthesis indicating clear scope and it’s not obvious what *p1++ means to some programmers (*p1++ is the same as *(p1++) due to ++ having higher precedence).

VN:F [1.9.22_1171]
Rating: 4.0/5 (1 vote cast)
C code to remove spaces from a string, 4.0 out of 5 based on 1 rating

19 responses to C code to remove spaces from a string

  1. neat

    VA:F [1.9.22_1171]
    0
  2. It's good, but not safe. If str isn't null terminated, the world might esplode.

    VA:F [1.9.22_1171]
    0
  3. Isn't that's what your teacher says to make sure you always null-terminate your string?

    I just found another gotcha in the code — It did not check for NULL pointer. If NULL is passed into the function, the world might explode this time.

    VA:F [1.9.22_1171]
    0
  4. void removeSpaces(char *array)
    {
    char *endPtr = array;
    while(*endPtr)
    {
    if(*endPtr == ' ')
    {
    endPtr++;
    }
    else
    {
    *array++ = *endPtr++;
    }
    }
    *array = '\0';
    }

    VA:F [1.9.22_1171]
    0
  5. The code is fine, not at the production level. Usually it is the responsibility of upper level code to take care of function limitations. For example NULL checking need not be performed in the function, eventhough no harm.

    In C every string should be NUL terminated. NUL is the only delimiter. If the string is not NULL terminated, it fails even at other functions that depends on NUL character of C strings.

    In C++ one can enforce the size of string using Class encapsulation mechanism.

    One correction, * and ++ fall in the same group of priority. ++ and * are having right to left associativity. Hence *p++ is interpreted as *(p++), i.e. increment the pointer in reserved place and dereference before updating pointer.

    Good work, keep it up. I would also suggest to post possible test cases (no need of code) thinking in destructive way of the code snippet, both from algorithm perspective and from language perspective.

    VA:F [1.9.22_1171]
    0
  6. Venki is right.
    a = *p++; is equal to

    a = *p;
    p++;

    VA:F [1.9.22_1171]
    0
  7. My idea is that, the code is good for discussion of the *p++, but not very efficient enough. There is no need to overwrite the same place with its own value. So I would agree with the Anonymous at 4th floor more.

    By the way, if NULL is passed in, no while condition can be true, the world is saved, right? Did I miss something?

    VA:F [1.9.22_1171]
    0
  8. Your code does not work for
    char * str=” abc de sdfws ssd sfdf sdfsd sds “;

    VN:F [1.9.22_1171]
    0
    • You must declare str as array notation, ie:
      char str[]=” abc de sdfws ssd sfdf sdfsd sds “;

      Declaring str as char * means that str cannot be modified. You will most likely get a seg fault if you try that.

      VN:F [1.9.22_1171]
      0
      • can you please explain why modifying a char *str=”blah blah” would cause a seg fault? thanks

        VA:F [1.9.22_1171]
        0
      • Well, I think str[] is a sort of constant pointer, while *str is modifiable…
        What I mean is, suppose :

        char str[]=”My name”;
        char *str2=”your name”;

        //Now

        str++; // illegal
        str2++; // legal.

        You can do this:
        *(str+3)=’M';

        But you can’t do this:
        str+3=str;

        And also you can edit str2 as:
        str2=str2+5;

        basically str is a unmodifiable lvalue, while str2 is modifiable lvalue…

        Please, explain me where I am wrong, if I am..

        VN:F [1.9.22_1171]
        0
  9. @1337c0d3r, love all your post! Though in this problem, you should probably insert a ” to mark the new end of the char array.

    void removespace(char *s){
    char *p = s;
    if(!*s) return;
    while((*s++)){
    if(*s == ‘ ‘) continue;
    *p++ = *s;
    }
    *p = ”;
    }

    VN:F [1.9.22_1171]
    0
  10. while (*p1++ = *p2++);

    how it will work….

    VN:F [1.9.22_1171]
    -1
  11. int i=0,j=0;

    while( str[i] != ”)
    {
    if(str[i]!=’ ‘)
    {
    str[j]=str[i];
    j++;
    }
    i++;

    }

    VA:F [1.9.22_1171]
    0
  12. In Java

    VN:F [1.9.22_1171]
    0
  13. I have a very out of the way solution:

    VN:F [1.9.22_1171]
    0

Leave a reply

Your email address will not be published. Required fields are marked *

You may use the <code> tag to embed your code.