有一个字符串开头或结尾含有n个空格(“ abcdefg ”)欲去掉前后空格,返回一个新字符串
#define _CRT_SECURE_NO_WARNINGS #include#include #include #include int getCount11(char* str, int* pCount) { int i, j = 0; char* p = str; int ncount = 0; if (str == NULL || pCount == NULL) { return -1; } i = 0; j = strlen(p) - 1; while (isspace(p[i]) && p[i] != ' ') { i++; } while (isspace(p[j]) && p[j] != ' ') { j--; } ncount = j - i + 1; *pCount = ncount; return 0; } //取出字符串前后的空格 int trimSpace(char* str, char* newstr) { int i, j = 0; char* p = str; int ncount = 0; if (str == NULL || newstr == NULL) { printf("程序出错n"); return -1; } i = 0; j = strlen(p) - 1; while (isspace(p[i]) && p[i] != ' ') { i++; } while (isspace(p[j]) && p[j] != ' ') { j--; } ncount = j - i + 1; strncpy(newstr, str + i, ncount); newstr[ncount] = ' '; return 0; } int main() { char* p = " abcdefg "; char buf[1024]={0}; int num = 0; getCount11(p, &num); trimSpace(p,buf); printf("%dn", num); printf("%s",buf); }



