谁能提供一个VC中文字符统计算法?
答案:2 悬赏:80 手机版
解决时间 2021-06-07 00:10
- 提问者网友:动次大次蹦擦擦
- 2021-06-06 18:31
给出任意字符串能输出其中中文字符数
最佳答案
- 五星知识达人网友:你哪知我潦倒为你
- 2021-06-06 19:13
void CCCharNumDlg::OnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(TRUE);
int len,num=0;
len = m_Edit.GetLength();
for(int i=0;i<len;i++)
{
char ch;
ch = m_Edit.GetAt(i);
if(ch<0 || ch>255)
num++;
}
num = num / 2;
CString str;
str.Format("中文字符个数是:%d个",num);
GetDlgItem(IDC_STATIC)->SetWindowText(str);
}
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(TRUE);
int len,num=0;
len = m_Edit.GetLength();
for(int i=0;i<len;i++)
{
char ch;
ch = m_Edit.GetAt(i);
if(ch<0 || ch>255)
num++;
}
num = num / 2;
CString str;
str.Format("中文字符个数是:%d个",num);
GetDlgItem(IDC_STATIC)->SetWindowText(str);
}
全部回答
- 1楼网友:思契十里
- 2021-06-06 20:40
#include "stdafx.h" #include <afxwin.h> //#include <string.h> //#include <stdio.h> #include <iostream> using namespace std; int main( void ) { CString str( L"%First Second#Third" ); CString resToken; int curPos= 0; resToken= str.Tokenize(L"% #",curPos); while (resToken != L"") { wcout.imbue(locale("chs")); //设置locale wcout<< resToken << endl; char buf[20]={'\0'}; WideCharToMultiByte(NULL,CP_ACP,resToken,resToken.GetLength(),buf,resToken.GetLength(),NULL,NULL); cout << buf << endl; resToken= str.Tokenize(L"% #",curPos); }; wchar_t text= L'我'; wcout.imbue(locale("chs")); wcout <<"this is "<<text << endl; return 0; }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯