One thing I noticed was some unnecessary math that could be eliminated by using better structured loops. For instance:
Code:
while (isspace(*a++));
a-=2;
while (*a++ != '\0'){
*string = *a;
string++;
}
Could be made more readable using something like:
Code:
while (isspace(*a)) a++;
while (*a != '\0'){
*string = *a;
string++, a++;
}
Otherwise, the function looks good. Here's another version of the function:
Code:
char * trim(char * str)
{
int length = strlen(str);
if(length > 0)
{
char * data = str;
char * front = str;
char * back = str[length];
while(isspace(*front)) ++front;
while(isspace(*(back-1))) --back;
while(front < back) *(data++) = *(front++);
*data = 0;
}
return str;
}