trim string function (code)
Hi all! I coded this function to eliminate spaces before and after a string.
e.g.:
Code:
trim(" just a test 123 ");
will return
But I didn't uderstand why the compiler ask for *trim instead of trim.
Any improvement/suggestion is welcome.
Code:
char *trim(char string[100])
{
int a, b, c, d;
char trimed_string[100];
a = b = c = d = 0;
for (a; a < strlen(string); a++)
{
if (string[a]!=' ' && !b) /* first valid char */
b = a;
if (string[a]==' ' && b)
{
for (d; d < strlen(string)-a;d++)
if (string[d+a]!=' ')
c = a+d;
}
}
a=0;
for (b; b <= c; b++)
{
trimed_string[a]= string[b];
a++;
}
trimed_string[ strlen(trimed_string)-10 ] ='\0'; // why -10 ? otherwise it'll return junk
return(trimed_string);
}
Re: trim string function (code)
>>trim(" just a test 123 ");
Don't forget, you cannot safely modify the data passed in by this function call, as its a string literal.