Skip to content

Instantly share code, notes, and snippets.

@neesenk
Created December 4, 2010 12:11
Show Gist options
  • Save neesenk/728134 to your computer and use it in GitHub Desktop.
Save neesenk/728134 to your computer and use it in GitHub Desktop.
char *strtrim(char *str)
{
size_t len;
char *pb;
for (pb = str; isspace(*pb); pb++); /* isspace(0) == 0 */
for (len = strlen(pb); len > 0 && isspace(pb[len - 1]); len--);
if (pb > str && len > 0)
memmove(str, pb, len);
str[len] = 0;
return str;
}
char *strtrim2(char *str)
{
char c = 0;
char *pb = str, *pe = str, *p = str;
while ((c = *p++)) {
if (isspace(c) && pb == str)
continue;
*pb++ = c;
if (!isspace(c)) pe = pb;
}
*pe = 0;
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment