有CString 字符串str="hello Beijing 您好北京 hello Beijing";单字如下:hello,Beijing,您,好,北,京,hello,Beijing;共8个,该如何计算?
解决方案 »
- MFCdialog ADO 连接Access 遇到问题了
- MFC 获取dataGrid当前行的值并显示在编辑框中
- MFC怎么进行窗口的转移?我现在已经写好了主窗口,想增加一个窗口,把这个窗口作为主窗口,原先那个主窗口变成子窗口应该怎么做呢?
- VC问题求助
- VC控件中按钮默认ID变为1001是什么原因?
- 要做酒类代理,大家帮忙起一个公司名字
- 请问 如何找到一个类成员函数的具体定义?
- 请问如何选中IE中一个列表的某一项呢。
- 无耻公司,竟然要我们周六来给公司搬家。
- 怎样分离时间字符串?提取2002-10-23中的年月日
- 如何写一个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);