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:
1 2 3 4 5 6 7 | void removeSpace(char *str) { char *p1 = str, *p2 = str; do while (*p2 == ' ') p2++; while (*p1++ = *p2++); } |
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).
C code to remove spaces from a string,
Anonymous said on October 20, 2010
neat
Anonymous said on November 28, 2010
It's good, but not safe. If str isn't null terminated, the world might esplode.
1337c0d3r said on November 29, 2010
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.
Anonymous said on December 24, 2010
void removeSpaces(char *array)
{
char *endPtr = array;
while(*endPtr)
{
if(*endPtr == ' ')
{
endPtr++;
}
else
{
*array++ = *endPtr++;
}
}
*array = '\0';
}
Venki said on May 5, 2011
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.
maxq said on July 3, 2011
Venki is right.
a = *p++; is equal to
a = *p;
p++;
moonus said on September 13, 2011
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?
bigwood said on November 16, 2011
Your code does not work for
char * str=” abc de sdfws ssd sfdf sdfsd sds “;
1337c0d3r said on November 16, 2011
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.
Balaji said on February 1, 2012
can you please explain why modifying a char *str=”blah blah” would cause a seg fault? thanks
1337c0d3r said on February 1, 2012
that’s because by declaring char *str, its data is stored in read-only data segment.
Nishant Mishra said on June 30, 2012
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..
UnderMonkey said on January 11, 2012
@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 = ”;
}
raghavG said on March 1, 2012
while (*p1++ = *p2++);
how it will work….
Akshay Khokhar said on May 18, 2012
int i=0,j=0;
while( str[i] != ”)
{
if(str[i]!=’ ‘)
{
str[j]=str[i];
j++;
}
i++;
}
vamsi said on September 20, 2012
In Java
kavish dwivedi said on September 24, 2012
I have a very out of the way solution:
Raja Chellappan said on January 17, 2013
Good one Kavish.
ANONYMOUS said on March 7, 2013
NOT WORKING!!!
EXTRA SMARTNESS