What is the best approach in stripping leading and trailing spaces in C?
This question looks as if it might be a homework question, so I'll answer obliquely: look up the man pages for isspace(3) and strlen(3) and use pointer arithmetic. Also depending on the problem at hand you may need malloc(3) to hold space for the result.
Don't forget that the representation of a C string includes a trailing 0 byte, often written '\0', which is not counted as part of the length of the string.
int i = strlen(s) - 1;
while (isspace(s[i]))
s[i--] = '\0';
while (isspace(*s))
s++;
That should take care of the problem as long as you don't care about mangling up the string like crazy and if you don't care about memory leaks!
You should be able to do it in-place; stripping whitespace can never cause the string to grow. You might be able to do it without first checking the length of the string, but doing so might be needlessly "clever". You should look into the memmove()
function, in addition to the ones @Norman Ramsey mentioned.
Here is how linux kernel does the trimming, called strstrip():
char *strstrip(char *s)
{
size_t size;
char *end;
size = strlen(s);
if (!size)
return s;
end = s + size - 1;
while (end >= s && isspace(*end))
end--;
*(end + 1) = '\0';
while (*s && isspace(*s))
s++;
return s;
}
Its basically a better formatted and error-checked version what the previous poster said.
You can do this entirely in place.
void stripLeadingAndTrailingSpaces(char* string){
assert(string);
/* First remove leading spaces */
const char* firstNonSpace = string;
while(*firstNonSpace != '\0' && isspace(*firstNonSpace))
{
++firstNonSpace;
}
size_t len = strlen(firstNonSpace)+1;
memmove(string, firstNonSpace, len);
/* Now remove trailing spaces */
char* endOfString = string + len;
while(string < endOfString && isspace(*endOfString))
{
--endOfString ;
}
*endOfString = '\0';
}
Here's a version using isspace:
char * trim(char *c) {
char * e = c + strlen(c) - 1;
while(*c && isspace(*c)) c++;
while(e > c && isspace(*e)) *e-- = '\0';
return c;
}
char *strstrip(char *s)
{
char *end;
while ( (*s) && isspace( *s))
s++;
if(!( *s) )
return s;
end = s;
while( ! *end)
end++;
end--;
while (end ! = s && isspace( *end))
end--;
*(end + 1) = '\0';
return s;
}
It is basically a more optimized code (in terms of speed & codesize ).
If we need to retain memory space then,
void strstrip(char *s)
{
char *start;
char *end;
start = s;
while ( (*start) && isspace( *start))
start++;
if(!( *start) )
{
*s='\0';
return ;
}
end = start;
while( ! *end)
end++;
end--;
while (end ! = start && isspace( *end))
end--;
*(end + 1) = '\0';
memmove(s, start, end-start+1);
return;
}
If you're on Linux/Windows and have the library glib linked into your program, you can use the the routine g_strstrip()
.
Here is a more concise and safer version of lakshmanaraj's first function:
#include <ctype.h>
char *mytrim(char *s)
{
if(s) { /* Don't forget to check for NULL! */
while(*s && isspace(*s))
++s;
if(*s) {
register char *p = s;
while(*p)
++p;
do {
--p;
} while((p != s) && isspace(*p));
*(p + 1) = '\0';
}
}
return(s);
}
while(s && isspace(s))
is redundant
(edit- I do not know why the asterisks in the above line are not visible)
while (isspace(*s))
does the same thing
has anyone knows how to write for any function whcich to delete all spaces from string.
Sorry for my bad english. :-((