Originally Posted by
BillyTKid
Code:
char *rtrim(char *s)
{
while( *s && s[strlen(s)-1]==' ' )
s[strlen(s)-1]=0;
return s;
}
char *ltrim(char *s)
{
while( *s==' ' )
memmove(s,s+1,strlen(s));
return s;
}
The ltrim function looks good, but why are you moving the whole string multiple times?
Code:
void ltrim(char *s)
{ char *t = s;
while( *t ==' ')
t++;
memmove(s,t,strlen(t)+1); }
The same with the rtrim function you only need 1 null terminator.
Code:
void rtrim(char *s)
{ int n;
for (n=strlen(s); n>0; n--)
if (s[n] != ' ')
{ s[n+1]='\0';
return; } }