如何计算中英文混合的CString中的单字数量?(在线等待) 有CString 字符串str="hello Beijing 您好北京 hello Beijing";单字如下:hello,Beijing,您,好,北,京,hello,Beijing;共8个,该如何计算? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 CString s("hello Beijing 您好北京 hello Beijing");static int num=0;for(int position=0;position<s.GetLength();position++){ position=s.Find(' ',position); num++;} 楼上的方法在一定程序上解决了以空格为分界的英文单词的个数,但觉得与楼主所说的要把每个汉字也要分出来的要求不符,但楼上的代码是可以参考的,但是还必须加上汉字判断的部分.现在VC中,汉字是每两个字符才能表示一个汉字的,而且第一个字符的ASCII码也是一个特别的值,只要判断这个值不在0到255之间,就可以断定这个字符是汉字编码的一部分,所以就必须把下面的那个字符也加进来并打印,这样就可以把每个汉字都分出来了. //用IsDBCSLeadByte(),关于此函数的说明请看MSDN//CString str("hello Beijing 您好北京 hello Beijing");int nLength = str.GetLength();int nCount = 0;for (int i = 0; i< nLength; i++){ if (str[i] == ' ') continue; else if (!IsDBCSLeadByte((BYTE)str[i])) nCount++;}//显示计算出来的单字符数CString strShow;strShow.Format("%d", nCount);AfxMessageBox(strShow);//结果显示为24 根据各位意见,整理以下要点1纯英文情况下:如"hello Beijing hello Beijing",空格数为3个,单词为4个;这种情况下计算出来的数需加12中英文混合情况下如"您好北京 hello Beijing",空格数为2个,中文单字为4个,共单字6个; 如"hello Beijing 您好北京 hello Beijing",空格数为4个,中文单字为4个,共单字8个; 如"hello Beijing 您好北京"; 空格数为2个,中文单字为4个,共单字6个;如"hello Beijing 您好北京 hello Beijing 您好北京",空格数为5个,中文单字8个,而单字共12个; 这种情况下计算出来的数需减13纯中文情况下如"您好北京",中文单字为4个,共单字数4个 正则表达式子搜索一下,问题不大表达式子可以是:[0-9a-zA-Z\u4e00-\u9fa5]+ //下午上班没事做,又看了一下。根据楼主的要求,写了段代码,加了对空格的判断。//对是否是中文,仍用IsDBCSLeadByte()来判断。CString str("hello Beijing 您好北京 hello Beijing "); int nLength = str.GetLength(); int nCount = 0;int nStart = 0;int nEnd = 0;//用来判断当找到空格又找到中文时,是否要调节字符的加1BOOL bAdjust = FALSE;for (nStart = 0; nStart < nLength; nStart++) { //查找空格 nEnd = str.Find(' ', nStart); //如果找到空格的位置是起始位置,则继续查找主要用 //来判断空格在字串开头或连续几个空格的情形 if (nEnd == nStart && nEnd != -1) continue; //如果找不到空格,把结束位置置为字串最后一个字符的位置 if (nEnd == -1) nEnd = nLength - 1; //取两个空格之间的字串并加以判断是否是中文 for (int i = nStart; i < nEnd; i++) { //是中文 if (IsDBCSLeadByte((BYTE)str[i])) { nCount++; i++;//中文是两字符,故此处要++ bAdjust = TRUE;//是中文,又以空格结束 } } //是英文 if (!bAdjust) nCount++; nStart = nEnd;//重置起始位置 bAdjust = FALSE; if (nStart == -1) break;} //显示计算出来的结果CString strShow; strShow.Format("%d", nCount); AfxMessageBox(strShow);//结果显示为8 有点难度,你要耐心点做,我有个想法:1,先以空格和标点分隔,如“hello Beijing 您好北京,hello.” 共4个字...2,然后分块统计字数,“您好北京”共4个字...(4+4-1)3,相加.“hello Beijing 您好北京,hello.” 共7(4+4-1)个字. 我也来一个 CString str="hello Beijing 您好北京 hello Beijing"; int nCount=0; int flag=0; int nTempCount=0; for(int i=0;i<str.GetLength();i++) { if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) { //是英文字母 if(flag==0) { nCount++; flag=1; } } else if(str[i]==' ') { //是空格 flag=0; } else { //剩下的应该就是汉字了 nTempCount++; } } nCount+=nTempCount/2; CString msg; msg.Format("总共%d个!",nCount); MessageBox(msg); Invalid property value错误//InvokeHelper 如何用图片填充整个对话框,包括标题栏 怎么加载皮肤库 如何设计自己得提示保存对话框 如何在内存中运行执行程序 如何改变拖放过程中的鼠标光标样式? CFtpConnection的问题。。。bug...,急急急急... 如何利用求文件md5值,找出重复的文件? 为什么说发送数据失败!哪出错了? 在for循环中定义的变量 如何写一个unicode 的错误报告函数, 用"可变参数"和"Messagebox" 请greatws 进来接分
CString s("hello Beijing 您好北京 hello Beijing");
static int num=0;
for(int position=0;position<s.GetLength();position++)
{
position=s.Find(' ',position);
num++;
}
//
CString str("hello Beijing 您好北京 hello Beijing");
int nLength = str.GetLength();
int nCount = 0;for (int i = 0; i< nLength; i++)
{
if (str[i] == ' ')
continue;
else if (!IsDBCSLeadByte((BYTE)str[i]))
nCount++;
}//显示计算出来的单字符数
CString strShow;
strShow.Format("%d", nCount);
AfxMessageBox(strShow);//结果显示为24
1纯英文情况下:
如"hello Beijing hello Beijing",空格数为3个,单词为4个;这种情况下计算出来的数需加12中英文混合情况下
如"您好北京 hello Beijing",空格数为2个,中文单字为4个,共单字6个;
如"hello Beijing 您好北京 hello Beijing",空格数为4个,中文单字为4个,共单字8个;
如"hello Beijing 您好北京"; 空格数为2个,中文单字为4个,共单字6个;
如"hello Beijing 您好北京 hello Beijing 您好北京",空格数为5个,中文单字8个,而单字共12个; 这种情况下计算出来的数需减13纯中文情况下
如"您好北京",中文单字为4个,共单字数4个
表达式子可以是:[0-9a-zA-Z\u4e00-\u9fa5]+
//对是否是中文,仍用IsDBCSLeadByte()来判断。
CString str("hello Beijing 您好北京 hello Beijing ");
int nLength = str.GetLength();
int nCount = 0;
int nStart = 0;
int nEnd = 0;//用来判断当找到空格又找到中文时,是否要调节字符的加1
BOOL bAdjust = FALSE;for (nStart = 0; nStart < nLength; nStart++)
{
//查找空格
nEnd = str.Find(' ', nStart);
//如果找到空格的位置是起始位置,则继续查找主要用
//来判断空格在字串开头或连续几个空格的情形
if (nEnd == nStart && nEnd != -1)
continue; //如果找不到空格,把结束位置置为字串最后一个字符的位置
if (nEnd == -1)
nEnd = nLength - 1; //取两个空格之间的字串并加以判断是否是中文
for (int i = nStart; i < nEnd; i++)
{
//是中文
if (IsDBCSLeadByte((BYTE)str[i]))
{
nCount++;
i++;//中文是两字符,故此处要++
bAdjust = TRUE;//是中文,又以空格结束
}
}
//是英文
if (!bAdjust)
nCount++; nStart = nEnd;//重置起始位置
bAdjust = FALSE;
if (nStart == -1)
break;
} //显示计算出来的结果
CString strShow;
strShow.Format("%d", nCount);
AfxMessageBox(strShow);//结果显示为8
1,先以空格和标点分隔,如“hello Beijing 您好北京,hello.” 共4个字...
2,然后分块统计字数,“您好北京”共4个字...(4+4-1)3,相加.“hello Beijing 您好北京,hello.” 共7(4+4-1)个字.
我也来一个 CString str="hello Beijing 您好北京 hello Beijing";
int nCount=0;
int flag=0;
int nTempCount=0;
for(int i=0;i<str.GetLength();i++)
{
if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z'))
{
//是英文字母
if(flag==0)
{
nCount++;
flag=1;
}
}
else if(str[i]==' ')
{
//是空格
flag=0;
}
else
{
//剩下的应该就是汉字了
nTempCount++;
}
}
nCount+=nTempCount/2;
CString msg;
msg.Format("总共%d个!",nCount);
MessageBox(msg);