1. 函数 char *trimspace(char *str)
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char *trimspace(char *str)
{
char *p = NULL;
if (!str || *str == '\0') {
return str;
}
// tirm prefix
p = str;
while (*p != '\0' && isspace(*p)) {
p++;
}
// tirm suffix
str = p;
p = str + strlen(str) - 1;
while (p >= str && isspace(*p)) {
p--;
}
*(p + 1) = '\0';
return str;
}
int main()
{
char buf[32] = {" \tabc \n"};
printf("orig: #%s#\n", buf);
printf("trim: #%s#\n", trim(buf));
return 0;
}