问题:
如何查找字符串中的小字符串个数?
比如在字符串 "abcadfbcaebf " 中 查找 'b '的个数或 "ca "的个数
===================================================================================
写法一:
int Substr(CString str,CString sub)
{
int nRet= 0, nStart = 0;
while(-1!=(nStart=str.Find(sub,nStart)))
{
nStart+=sub.GetLength();
++nRet;
}
return nRet;
}
usage:
int n=Substr( "abcadfbcaebf ", "b ");
int n=Substr( "abcadfbcaebf ", "ca ");
==================================================================
另外一种写法:
CString str;
str = "abcadfbcaebf ";
int nCount1=0, nCount2=0;
for(int i=0; i <str.GetLength(); i++)
{
if(str[i] == 'b ')
nCount1++;
}
while( !str.IsEmpty() )
{
int index;
if( (index = str.Find( "ca ")) != -1)
{
nCount2++;
str = str.Mid(index+2);
}
else
break;
}
============================================================================================
写法三:使用Hash表,Map:http://wenwen.soso.com/z/q278570518.htm
#pragma warning(disable:4786)
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
typedef map<char,int> MAP;
void print(MAP::value_type t)
{
cout<<t.first<<": 有"<<t.second<<"个"<<endl;
}
void CountCharacter(MAP &m,char *szText)
{
char *p=szText;
if (!p)
return;
while (*p)
{
MAP::iterator it=NULL;
it=m.find(*p);
if (it==m.end())
{
m.insert(MAP::value_type(*p,1));
}
else
{
++(it->second);
}
++p;
}
return;
}
int main()
{
MAP myMap;
char szText[512]={0};
cout<<"请输入一字符串:"<<endl;
fgets(szText,512,stdin);
szText[strlen(szText)-1]=0;
CountCharacter(myMap,szText);
for_each(myMap.begin(),myMap.end(),print);
return 0;
}
如何查找字符串中的小字符串个数?
比如在字符串 "abcadfbcaebf " 中 查找 'b '的个数或 "ca "的个数
===================================================================================
写法一:
int Substr(CString str,CString sub)
{
int nRet= 0, nStart = 0;
while(-1!=(nStart=str.Find(sub,nStart)))
{
nStart+=sub.GetLength();
++nRet;
}
return nRet;
}
usage:
int n=Substr( "abcadfbcaebf ", "b ");
int n=Substr( "abcadfbcaebf ", "ca ");
==================================================================
另外一种写法:
CString str;
str = "abcadfbcaebf ";
int nCount1=0, nCount2=0;
for(int i=0; i <str.GetLength(); i++)
{
if(str[i] == 'b ')
nCount1++;
}
while( !str.IsEmpty() )
{
int index;
if( (index = str.Find( "ca ")) != -1)
{
nCount2++;
str = str.Mid(index+2);
}
else
break;
}
============================================================================================
写法三:使用Hash表,Map:http://wenwen.soso.com/z/q278570518.htm
#pragma warning(disable:4786)
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
typedef map<char,int> MAP;
void print(MAP::value_type t)
{
cout<<t.first<<": 有"<<t.second<<"个"<<endl;
}
void CountCharacter(MAP &m,char *szText)
{
char *p=szText;
if (!p)
return;
while (*p)
{
MAP::iterator it=NULL;
it=m.find(*p);
if (it==m.end())
{
m.insert(MAP::value_type(*p,1));
}
else
{
++(it->second);
}
++p;
}
return;
}
int main()
{
MAP myMap;
char szText[512]={0};
cout<<"请输入一字符串:"<<endl;
fgets(szText,512,stdin);
szText[strlen(szText)-1]=0;
CountCharacter(myMap,szText);
for_each(myMap.begin(),myMap.end(),print);
return 0;
}