How to delete the space characters from the string

J

jayapal

Hi all,

Is there any library function in order to delete the space characters
from the given stirng.

Thanks,
Jay
 
S

santosh

jayapal said:
Hi all,

Is there any library function in order to delete the space characters
from the given stirng.

Not in Standard C, but it's pretty easy to roll your own. If you are
having problems doing so, then by all means post your code along with
your question.

PS. If you do "roll your own" you might want to make the function more
generic by designing it to remove all occurrences of a user specified
character, rather than creating it for the specific case of a space
character alone.
 
M

Mark Bluemel

jayapal said:
Hi all,

Is there any library function in order to delete the space characters
from the given stirng.

How about looking in the standard?

The answer is "no", anyway. But it wouldn't be difficult to write one,
would it?

Untested code follows:-

/* "squeeze a string "in place" */
char *removeSpaces(char * string) {
char *source = string;
char *target = string;

while(*source) {
if (*source != ' ') {
*target++ = *source;
}
source++;
}
*target = '\0';
return string;
}
 
R

Richard

Mark Bluemel said:
How about looking in the standard?

So we move from "RTFM" to "why don't you look in the standard". Despite
the standard (standard, not the draft )not being available for free
online and almost inaccessible to anyone without formal CS training.
The answer is "no", anyway. But it wouldn't be difficult to write one,
would it?

Possibly for a C noob...
Untested code follows:-

/* "squeeze a string "in place" */
char *removeSpaces(char * string) {
char *source = string;
char *target = string;

/*alternatively and as untested something like the following which uses
natural termination like strcpy.*/

char c;
while(*target = *source++)
if(*target)!=' ')
target++;

/*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top