Skip to content

Instantly share code, notes, and snippets.

@reportbase
Created March 22, 2015 21:09
Show Gist options
  • Save reportbase/54e7b29ddf2c46f16e69 to your computer and use it in GitHub Desktop.
Save reportbase/54e7b29ddf2c46f16e69 to your computer and use it in GitHub Desktop.
trim strings
inline char* ltrim(char* const s)
{
int len;
char* cur;
if(s && *s)
{
len = strlen(s);
cur = s;
while(*cur && isspace(*cur))
++cur, --len;
if(s != cur)
memmove(s, cur, len + 1);
}
return s;
}
inline char* rtrim(char* const s)
{
int len;
char* cur;
if(s && *s)
{
len = strlen(s);
cur = s + len - 1;
while(cur != s && isspace(*cur))
--cur, --len;
cur[isspace(*cur) ? 0 : 1] = '\0';
}
return s;
}
inline char* trim(char* const s)
{
rtrim(s);
ltrim(s);
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment