forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.43.cpp
39 lines (35 loc) · 1.21 KB
/
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
39
#include <string>
#include <iostream>
void replaceStr(std::string &s,
const std::string &oldVal,
const std::string &newVal) {
// Use `<` operator, because there is possibility that `it` jump over `end()`
for (auto it = s.begin(); it < s.end() - oldVal.size() + 1; ) {
auto it2 = oldVal.cbegin();
for (auto it3 = it; it2 != oldVal.cend(); ++it2, ++it3)
if (*it3 != *it2)
break;
if (it2 == oldVal.cend()) {
std::string::size_type pos = it - s.begin();
s.erase(pos, oldVal.size());
s.insert(pos, newVal);
// Recalculate `it` from `begin()` rather than use `+=` operator, because
// `it` is invalid by `erase()` and `insert()`
it = s.begin() + pos + newVal.size();
} else
++it;
}
}
int main() {
std::string s{"r u ok?\ngo thru\ntho tho altho\nthrough thruu"};
std::cout << "Old:\n" << s << std::endl;
replaceStr(s, "tho", "though");
std::cout << "\nNew:\n" << s << std::endl;
replaceStr(s, "thru", "through");
std::cout << "\nNew:\n" << s << std::endl;
replaceStr(s, "hl", "hello");
std::cout << "\nNew:\n" << s << std::endl;
replaceStr(s, "u", "you");
std::cout << "\nNew:\n" << s << std::endl;
return 0;
}