Writing String.trim() in C

Posted by Phoenix on Stack Overflow See other posts from Stack Overflow or by Phoenix
Published on 2010-03-15T21:42:42Z Indexed on 2010/03/15 21:49 UTC
Read the original article Hit count: 326

Filed under:
|

Hi guys, I was writing the String trim method in c and this is the code I came up with. I think it does the job of eliminating leading and trailing whitespaces however, I wish the code could be cleaner. Can you suggest improvements.

void trim(char *String)
{
int i=0;j=0;
char c,lastc;
while(String[i])
{
   c=String[i];
   if(c!=' ')
   {
     String[j]=c;
     j++;
   }
   else if(lastc!= ' ')
   {
     String[j]=c;
     j++;

   }
   lastc = c;
   i++;
}

Does this code look clean ??

© Stack Overflow or respective owner

Related posts about trim

Related posts about c