清除字符串开头结尾除空白
修改原字符串
不能清除全角字符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/*
* 清除字符串开始结尾处的空白
* 修改原字符串
* */
char * p1 = str,*p2 = NULL,*p3 = NULL;
int flag = 0;
while (*(p1) != '\0' ){
if ( isspace (*p1)){
if (p3 == NULL) p3 = p2 == NULL?p1:p2; //标记结尾空白
if (0 == flag) { //标记开头为空白
flag = 1;
p2 = p1;
} else if (2 == flag){ //中间空白
*(p2++) = *p1;
}
} else {
if (1 == flag || flag == 2){ //开头空白结束标志
*(p2++) = *p1;
flag = 2;
} else flag = -1; //开头无空白
p3 = NULL; //清除结尾空白
}
p1++;
}
//标记结尾
if (p3 != NULL) *p3 = '\0' ;
else if (p2 != NULL) *p2 = '\0' ;
return str;
}
|