Wednesday, June 21, 2006

堪称经典的超强TRIM函数实现

只用了一个循环两个变量

void trim( char *str )
{
char *copied, *tail = NULL;

if ( str == NULL )
return;

for( copied = str; *str; str++ )
{
if ( *str != ' ' && *str != '\t' )
{
*copied++ = *str;
tail = copied;
}
else
{
if ( tail )
*copied++ = *str;
}
}

if ( tail )
*tail = 0;
else
*copied = 0;

return;
}

Thursday, June 15, 2006

C/C++ Reference