user-0 提问: 请大神看看这个去字符窜左右空格的函数的bug在那里？

回复:
user: user-1
content: 那个最后的拷贝操作不需要， 不然就不需要返回值了， char *mytrim(char *s){ 	if(!s) return s;  	char *e =s + strlen(s)-1; 	for(;e>=s&&isspace(*e);--e); 	e[1] ='\0'; 	for(;*s&&isspace(*s);++s); 	return s; }

user: user-2
content: 1、空白符应该也包含了'\n'才对，用isspace函数来判别比较好； 2、strncpy(s,s+i,len-i)中s和s+i空间覆盖了，用一个临时指针中转吧。

user: user-3
content: 仅供参考 char *mytrim(char *s) { 	if (NULL == s || strlen(s) == 0) 	{ 		return s; 	} 	char *p1 = s;		// 循环后使 p 指向第一个非空格字符 	while (*p1 && *p1 == ' ') 	{ 		p1++; 	} 	char *p2 = s + strlen(s) - 1;	// 循环后使p2指向最后一个非空格字符 	while (*p2 && *p2 == ' ') 	{ 		p2--; 	} 	*(p2+1) = '\0';		// 其后设置为结束符 	return p1;		// 返回以p1开始的字符指针，注意此函数只改变了原串的结束符位置（去掉了末尾的空格），没有去掉原串s前边的空格 }

user: user-4
content: 我的意思是你用isspace而不setlocale是不成熟的表现。

user: user-5
content: 引用 7 楼 zhao4zhong1 的回复:[quote=引用 6 楼 sholber 的回复:] 上面的一律差评！哼！ char * mytrim (char * s) {     char * t;      if (s == NULL || (t = s + strlen(s)) == s) return NULL;     while (* s != '\0' && isspace (* s)) s ++;     while (-- t > s && isspace (* t)) * t = '\0';     return s; } Locale Use the setlocale function to change or query some or all of the current program locale information. “Locale” refers to the locality (the country and language) for which you can customize certain aspects of your program. Some locale-dependent categories include the formatting of dates and the display format for monetary values. For more information, see Locale Categories.   Locale-Dependent Routines   Routine  Use setlocale Category  Setting Dependence  atof, atoi, atol Convert character to floating-point, integer, or long integer value, respectively LC_NUMERIC  is Routines Test given integer for particular condition. LC_CTYPE  [/quote]  In the "C"locale,isspacereturns true only for the standard white-space characters. 给学生出这样的题本身就是不科学的。 题目本身需要的知识远远超过了它所要考察的C语言技能。

user: user-4
content: 引用 6 楼 sholber 的回复:上面的一律差评！哼！ char * mytrim (char * s) {     char * t;      if (s == NULL || (t = s + strlen(s)) == s) return NULL;     while (* s != '\0' && isspace (* s)) s ++;     while (-- t > s && isspace (* t)) * t = '\0';     return s; } Locale Use the setlocale function to change or query some or all of the current program locale information. “Locale” refers to the locality (the country and language) for which you can customize certain aspects of your program. Some locale-dependent categories include the formatting of dates and the display format for monetary values. For more information, see Locale Categories.   Locale-Dependent Routines   Routine  Use setlocale Category  Setting Dependence  atof, atoi, atol Convert character to floating-point, integer, or long integer value, respectively LC_NUMERIC  is Routines Test given integer for particular condition. LC_CTYPE

user: user-5
content: 上面的一律差评！哼！ char * mytrim (char * s) {     char * t;      if (s == NULL || (t = s + strlen(s)) == s) return NULL;     while (* s != '\0' && isspace (* s)) s ++;     while (-- t > s && isspace (* t)) * t = '\0';     return s; }

user: user-6
content: 引用 1 楼 u010936098 的回复:第一个循环结束后最好加一句： len = i + 1;  最大问题出现在strncpy(s,s+i,len-i)这句上。MSDN上说，如果strncpy的源和目标字符串有重叠区域，则其行为“未定义”，所以不能用这个函数，不妨改成用循环实现。+1

user: user-7
content: 第一个循环结束后最好加一句： len = i + 1;  最大问题出现在strncpy(s,s+i,len-i)这句上。MSDN上说，如果strncpy的源和目标字符串有重叠区域，则其行为“未定义”，所以不能用这个函数，不妨改成用循环实现。