-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-functions.cpp
More file actions
44 lines (35 loc) · 814 Bytes
/
string-functions.cpp
File metadata and controls
44 lines (35 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
bool checkString(string, string, int&);
int main()
{
int num = 0;
string str1 = "Hello my my my my name is Matthew";
string str2 = "my";
if (checkString(str1, str2, num))
{
cout << "Found: It occured \"" << num << "\" time(s)." << endl;
}
else {
cout << "Not found" << endl;
}
return 0;
}
bool checkString(string str1, string str2, int& num)
{
cout << string::npos << endl;
cout << str1.find(str2) << endl;
cout << str2.length() << endl;
int pos = 0;
if (str1.find(str2) != string::npos)
{
while ((pos = str1.find(str2, pos)) != string::npos) {
++num;
pos += str2.length() - 1;
}
return true;
}
else {
return false;
}
}