//去除字符串两头的空格
void Space(char *soucre) {
if (soucre == NULL) {
printf("delSpace: 传参错误");
return;
}
//strlen() 计算的时数据的真实长度 ,不包含' '
char* pev = soucre; //字符串前指针
//strlen 计算的总数, str下标从0开始,-1后正好===总数
char* end = soucre + (strlen(soucre)-1); //字符串的从后向前找空字符的指针
char Buf[100] = { 0 }; //最后找到的 有效字符串
//isspace 为空返回非0
//为空则指针向后移动一位
//pev==end 说明指针已经到最后了,退出循环,不然下标越界
//只有前面有空格或者没有有效字符
while (isspace(*pev) && pev <= end) { pev++; };
//为空则指针向前移动一位 ,
//end == pev 说明字符串只有后面有空格
while (isspace(*end) && end >= pev) { end--; };
int len = 0;
//字符串中就一个有效字符的话pev 会和end 重逢
//pev<=end 说明最少有一个有效字符
if (pev <= end) {
//最后end 会停留在有效字符上 ,有效字符本身占一位,要+1
len = end - pev + 1;
strncpy(Buf, pev, len);
printf("BUF = %sn", Buf);
}
else {
printf("没有有效字符n");
}
}