#include <stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>//Delete the contiguous spaces on the right,Char* RTRIM (Char*pstr) { Char*p =pstr; intLen =strlen (PSTR); P+ = Len-1; for(; isspace (*p) && len >0; P--, len--) { *p =' /';//truncate } returnpstr;}intMain () {CharText[] = {"d:\\ My Documents"}; printf ("text:%s, length:%d\n", Text,strlen (text)); Char*p =RTrim (text); printf ("text:%s, length:%d\n", P,strlen (p)); return 0;}
Output result: (why Chinese [document] Two words will output garbled?)
---------------------------------------
Text:d:\ My Documents, length:13
Text:d:\ to me? Length:8
Check for half a day, estimated to be isspace () problem, do not use Isspace (), Overwrite:
for (; (*p = = ' | | *p = = ' \ t ' | | *p = = ' \ n ' | | *p = ' \ r ') && len > 0; P--, len--)
{
*p = ' + '; Truncated
}
Test again, it's normal.
Output Result:
---------------------------------------
Text:d:\ My Documents, length:13
Text:d:\ My Documents, length:11
Continued:
A few days later and unexpectedly found that in the processing of ' word ' at the beginning of the Chinese language, will also appear garbled.
It seems that the future processing of Chinese strings, can not use the isspace () function, too dangerous.
Isspace is not normal when checking strings that contain Chinese characters!?