Code:
Example input:
" Foo Bar Xip Bas "
Output:
"Foo Bar Xip Bas"
My function just shifts each of the characters by a certain number of places, which is determined by the number of spaces already encountered.
Code:
char* alltrim(char* s)
{
int l=strlen(s);
int shift=0,i,prev=0,last=-1;
for(i=0;i<l;++i)
{
//fprintf(stderr,"i=%d\tshift=%d\tprev=%d,\tlast=%d\n",i,shift,prev,last);
if(isspace(s[i]))
{
if(!prev)
shift++;
prev=0;
}
else
{
if(shift)
{
s[i-shift]=s[i];
s[i]=' ';
}
prev=1;
last=i-shift;
}
}
s[last+1]='\0';
return s;
}
Btw, this works only when I pass it a char [] but not for char*, where it segfaults (when shifting ) -- why does that happen?