C applet - extract the memory space data pointed to by p (remove the leading and trailing spaces)

//remove spaces

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;

}

// test case

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325901523&siteId=291194637