Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
1
#include <iostream>
2
#include <string>
3
#include <vector>
4
5
using namespace std;
6
7
vector<int> findAll(const string &haystack, const string &needle)
8
{
9
vector<int> result;
10
size_t pos = 0;
11
12
do {
13
pos = haystack.find(needle, pos);
14
if(pos!=string::npos) {
15
result.push_back(pos);
16
pos++;
17
}
18
} while(pos != string::npos);
19
20
21
return result;
22
}
23
24
int main()
25
{
26
string haystack, needle;
27
getline(cin, haystack);
28
getline(cin, needle);
29
30
vector<int>locations = findAll(haystack, needle);
31
for(auto itr = locations.begin(); itr!= locations.end(); itr++) {
32
cout << *itr << endl;
33
}
34
35
return 0;
36
}
37
38