如何用C++判断一个字符串是否为中文

如题所述

每个汉字由2个小于0的ASCLL码组成,所以只要判断字符的ASCLL是否都小于0,并且字符数是偶数。例如:
#include <iostream>
using namespace std;
int main()
{
char str[100];
bool ischs=true;
cin>>str;
if(strlen(str)%2==0)
{
for(int i=0;i<strlen(str);i++)
{
if(str[i]>=0) { ischs=false; break; }
}
}
else ischs=false;
if(ischs) cout<<"全部都是中文!"<<endl;
else cout<<"不是全部都是中文!"<<endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-13
这是判断字符串中是否有中文的代码

#include <stdio.h>
#include <string.h>
int main()
{
char sztext[] = " 是ciw.";
char c = 0;
char szchinese[3] = {0};
int i = 0, nlen = strlen(sztext);
for(; i < nlen; i++)
{
if( sztext[i] >= 0 && sztext[i] <= 127 ) //不是全角字符?
c = sztext[i], printf("%c\n", c);
else //是全角字符
szchinese[0] = sztext[i], szchinese[1] = sztext[i + 1], printf("%s\n", szchinese), i++; //中文是2个字节,所以i++
}
return 0;
}本回答被网友采纳
第2个回答  2012-08-13
很简单,思路就是中文字符的ASCII是小于0的
第3个回答  2012-08-13
判断连续两个字符的acsii码是否大于0x7f,若是基本上可以确定这两个字符代表了一个字
相似回答