Skip to content

Instantly share code, notes, and snippets.

@kenkam
Created January 21, 2011 17:58
Show Gist options
  • Save kenkam/790090 to your computer and use it in GitHub Desktop.
Save kenkam/790090 to your computer and use it in GitHub Desktop.
A simple function to trim some space off a string in C
char * trim_space(char *str) {
char *end;
/* skip leading whitespace */
while (isspace(*str)) {
str = str + 1;
}
/* remove trailing whitespace */
end = str + strlen(str) - 1;
while (end > str && isspace(*end)) {
end = end - 1;
}
/* write null character */
*(end+1) = '\0';
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment