首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-02-29 
请教:这样判断字符串中汉字的个数准确吗?#include iostreamusingstd::cinusingstd::coutusingstd::end

请教:这样判断字符串中汉字的个数准确吗?
#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>

热点排行