读书人

请问:这样判断字符串中汉字的个数准确

发布时间: 2012-02-29 16:44:10 作者: rapoo

请教:这样判断字符串中汉字的个数准确吗?
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int first(unsigned char ch)
{
if(ch> =0xb0&&ch <=0xf7)
return 1;
else
return 0;
}
int second(unsigned char ch)
{
if(ch> =0xa1&&ch <=0xfe)
return 1;
else
return 0;
}



main() {
int n;
int N=0;
cin> > n;
while(N!=n){
char a[100];
int count=0;
cin> > a;
for(int i=0;i <strlen(a)-1;i++){
if(first(a[i])&&second(a[i+1])){
count++;
i++;
}
}
cout < <count < <endl;
N++;
}
return 0;



}


[解决办法]
呃汉字的最高位为1
其实你可以先将字符串转化为unicode然后wcslen 一个结果
在用原始的结果strlen看看差为多少就可以了
[解决办法]
不准确!!
wcslen(_T(yonrString)) - strlen(yourString)

yourString为你要求的字符串。
[解决办法]
#include <tchar.h>

读书人网 >C++

热点推荐