功能:去掉字符串首尾的空格,換行符等空白。spa
代碼:code
#include <string.h> #include <stdio.h> #include <ctype.h> char *trim(char *str) { char *p = str; char *p1; if(p) { p1 = p + strlen(str) - 1; while(*p && isspace(*p)) p++; while(p1 > p && isspace(*p1)) *p1--=0; } return p; } int main() { char a[]=" asa "; char* h=trim(a); printf("%s\n",h); return 0; }
ps:不能直接用char* a=" asd ";由於這是常量字符串,不能修改。blog