-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exer09_43.cpp
34 lines (34 loc) · 887 Bytes
/
Exer09_43.cpp
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
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
string& changeSubstr(string &s, const string& oldVal, const string& newVal)
{
if(s.empty() || oldVal.empty()) // be careful, s and oldVal should be non empty strings
return s;
auto sizeOld = oldVal.size();
auto sizeNew = newVal.size();
auto iter = s.begin();
while(iter != s.end()) // make sure don't skip any element
{
string subOld(iter, iter + sizeOld);
if(subOld == oldVal)
{
s.erase(iter, iter + sizeOld);
iter = s.insert(iter, newVal.begin(), newVal.end());
iter += sizeNew;
}
else
++iter;
}
return s;
}
int main()
{
string s = "I came thru the way. thru thru thru";
cout << s << endl;
changeSubstr(s, "", "");
cout << s << endl;
return 0;
}