-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path9.43.cpp
38 lines (34 loc) · 1001 Bytes
/
9.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
35
36
37
38
/*
* Exercise 9.43: Write a function that takes three strings, s, oldVal, and
* newVal. Using iterators, and the insert and erase functions replace all
* instances of oldVal that appear in s by newVal. Test your function by using
* it to replace common abbreviations, such as “tho” by ”though” and ”thru” by
* “through”.
*
* By Faisal Saadatmand
*/
#include <iostream>
#include <string>
std::string &replace_with(std::string &s,
const std::string &oldVal, const std::string &newVal)
{
for (auto it = s.begin(); it != s.end(); ++it) {
auto pos = it; // store in case of a match
auto pattern = oldVal.cbegin();
for ( ; *it == *pattern; ++pattern)
++it;
if (pattern == oldVal.cend()) {
pos = s.erase(pos, it);
it = s.insert(pos, newVal.cbegin(), newVal.cend());
}
}
return s;
}
int main()
{
std::string str("tho and thru but not u");
replace_with(str, "tho", "though");
replace_with(str, "thru", "through");
std::cout << str << '\n';
return 0;
}