Number of small strings in C + + statistics string
Source: Internet
Author: User
Problem:
How to find the number of small strings in a string.
For example, find the number of ' B ' in the string "ABCADFBCAEBF" or the number of "Ca"
===================================================================================
Writing a:
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");
==================================================================
Another way of writing:
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
}
============================================================================================
Using a hash table, map:http://wenwen.soso.com/z/q278570518.htm
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service