C小程序—提取p所指内存空间数据(去掉前后空格)

//求去掉空格

int trimSpaceStr2( char *p, char *buf2)

{
int ret = 0;
int ncount = 0;
int i, j;
i = 0;
j = strlen(p) -1;
while (isspace(p[i]) && p[i] != '\0')
{
i++;
}
while (isspace(p[j]) && j>0 )
{
j--;
}
ncount = j - i + 1;
//
strncpy(buf2, p+i, ncount);
buf2[ncount] = '\0';

return ret;

}

//测试用例

void main()
{
char buf[] = "     abcd     ";
char buf2[1024] = {0};
//memset(buf2, 0, sizeof(buf2));
printf("buf2:%s \n", buf2);
}

猜你喜欢

转载自blog.csdn.net/lanlan1266/article/details/80226632