Isspace is not normal when checking strings that contain Chinese characters!?

Source: Internet
Author: User
Tags rtrim

#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!?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.