Skip to content

Instantly share code, notes, and snippets.

@AlecTaylor
Last active September 11, 2018 04:35
Show Gist options
  • Save AlecTaylor/a7d4c3a9ab79d4e4fcebc9bcf4f49b7b to your computer and use it in GitHub Desktop.
Save AlecTaylor/a7d4c3a9ab79d4e4fcebc9bcf4f49b7b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
static inline void trim(char *&);
int main(int argc, char *argv[]) {
char *s = " foo bar ";
trim(s);
printf("s = \"%s\"\n", s);
}
static inline void *trim(char *&s) {
char *end;
while (isspace((unsigned char) *s) || ispunct((unsigned char) *s)) s++;
if (*s == 0)
return s;
end = s + strlen(s) - 1;
while (end > s && isspace((unsigned char) *end) && ispunct((unsigned char) *s)) end--;
end[1] = '\0';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment