请问我怎么获得Hello的个数?
8 个解决方案
#1
子串匹配 然后成功就加1
#2
使用string的find方法依次查找
#3
查找子字符串。
#4
求问"aaaa"里面有几个"aa"呢?
2或3?
2或3?
#5
经过验证,供参考:
#include <iostream>
#include <string>
using namespace std;
#define MAX 100
int main(int argc, char* argv[])
{
char temp[MAX];
string str;
string pattern;
size_t pos;
int number = 0;
cout << "Please enter a string:" << endl;
memset(temp, 0, MAX);
cin.getline(temp, 100);
str = temp;
cout << "Please enter a pattern to search in the above entered string:" << endl;
memset(temp, 0, MAX);
cin.getline(temp, 100);
pattern = temp;
pos = 0;
while(pos != string::npos)
{
pos = str.find(pattern, pos);
if(pos != string::npos)
{
++number;
pos += (pattern.size() + 1);
}
}
cout << "There are " << number << " '" << pattern << "'s in string." << endl;
return 0;
}
#6
应该是最大匹配原则吧。
#7
给5楼增加注释:
#include <iostream>
#include <string>
using namespace std;
#define MAX 100
int main(int argc, char* argv[])
{
char temp[MAX]; // 字符串最大长度为MAX
string str; // 字符串
string pattern; // 匹配字符串
size_t pos; // 在字符串中位置
int number = 0; // 匹配次数
cout << "Please enter a string:" << endl;
memset(temp, 0, MAX);
cin.getline(temp, MAX); // 可以包含空格,如果直接用cin>>str,就不能包含空格了
str = temp;
cout << "Please enter a pattern to search in the above entered string:" << endl;
memset(temp, 0, MAX);
cin.getline(temp, MAX); // 匹配字符串也可以包含空格,如果直接用cin>>pattern,就不能包含空格了
pattern = temp;
pos = 0;
while(pos != string::npos)
{
pos = str.find(pattern, pos);
if(pos != string::npos)
{
++number; // 如果在str中找到一个pattern,number就加1
pos += (pattern.size() + 1); // 在str中指向下一个匹配起始点
}
}
cout << "There are " << number << " '" << pattern << "'s in string." << endl;
return 0;
}
#8
#include <stdio.h>
#include <Windows.h>
#include <atlstr.h>
int cout = 0;
int num(CString str)
{
int i = str.Find("Hello");
if (i != -1)
{
cout++;
CString tmp = str.Mid(i+5, str.GetLength() - i -5);
num(tmp);
}
return cout;
}
int main()
{
CString str= "Hello word !Hello word !Hello word !Hello word";
int total = num(str);
printf("num is %d \n",total);
}
#1
子串匹配 然后成功就加1
#2
使用string的find方法依次查找
#3
查找子字符串。
#4
求问"aaaa"里面有几个"aa"呢?
2或3?
2或3?
#5
经过验证,供参考:
#include <iostream>
#include <string>
using namespace std;
#define MAX 100
int main(int argc, char* argv[])
{
char temp[MAX];
string str;
string pattern;
size_t pos;
int number = 0;
cout << "Please enter a string:" << endl;
memset(temp, 0, MAX);
cin.getline(temp, 100);
str = temp;
cout << "Please enter a pattern to search in the above entered string:" << endl;
memset(temp, 0, MAX);
cin.getline(temp, 100);
pattern = temp;
pos = 0;
while(pos != string::npos)
{
pos = str.find(pattern, pos);
if(pos != string::npos)
{
++number;
pos += (pattern.size() + 1);
}
}
cout << "There are " << number << " '" << pattern << "'s in string." << endl;
return 0;
}
#6
应该是最大匹配原则吧。
#7
给5楼增加注释:
#include <iostream>
#include <string>
using namespace std;
#define MAX 100
int main(int argc, char* argv[])
{
char temp[MAX]; // 字符串最大长度为MAX
string str; // 字符串
string pattern; // 匹配字符串
size_t pos; // 在字符串中位置
int number = 0; // 匹配次数
cout << "Please enter a string:" << endl;
memset(temp, 0, MAX);
cin.getline(temp, MAX); // 可以包含空格,如果直接用cin>>str,就不能包含空格了
str = temp;
cout << "Please enter a pattern to search in the above entered string:" << endl;
memset(temp, 0, MAX);
cin.getline(temp, MAX); // 匹配字符串也可以包含空格,如果直接用cin>>pattern,就不能包含空格了
pattern = temp;
pos = 0;
while(pos != string::npos)
{
pos = str.find(pattern, pos);
if(pos != string::npos)
{
++number; // 如果在str中找到一个pattern,number就加1
pos += (pattern.size() + 1); // 在str中指向下一个匹配起始点
}
}
cout << "There are " << number << " '" << pattern << "'s in string." << endl;
return 0;
}
#8
#include <stdio.h>
#include <Windows.h>
#include <atlstr.h>
int cout = 0;
int num(CString str)
{
int i = str.Find("Hello");
if (i != -1)
{
cout++;
CString tmp = str.Mid(i+5, str.GetLength() - i -5);
num(tmp);
}
return cout;
}
int main()
{
CString str= "Hello word !Hello word !Hello word !Hello word";
int total = num(str);
printf("num is %d \n",total);
}