Re: Comment on trim string function please



Doing such things as returning malloc'ed memory, checking for NULL, or
returning a pointer to the first non-space will generally lead to
misuse. Just keep it simple:

char*
trim( char* str )
{
char
* cpy = str,
* seq = str,
* fin = str + strlen( str ) - 1;
while( seq <= fin && isspace( *seq ) )
++seq;
while( seq <= fin && isspace( *fin ) )
--fin;
while( seq <= fin )
*cpy++ = *seq++;
*cpy = 0;
return str;
}

I can't guarantee that it's bug free of course...
.



Relevant Pages