Skip to content

Commit

Permalink
Update ex9_44.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy committed Oct 21, 2015
1 parent 5e5aedc commit a803715
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions ch09/ex9_44.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@
//
// @Brief Rewrite the previous function using an index and replace.
// @See 9.43
//
// Refactored by Yue Wang Oct, 2015
//

#include <iostream>
#include <string>

using std::cout; using std::endl; using std::string;
using std::cout;
using std::endl;
using std::string;

void func(string &s, string const& oldVal, string const& newVal)
auto replace_with(string &s, string const& oldVal, string const& newVal)
{
for (size_t pos = 0; pos <= s.size() - oldVal.size();) {
if (s[pos] == oldVal[0] && s.substr(pos, oldVal.size()) == oldVal) {
s.replace(pos, oldVal.size(), newVal);
for (size_t pos = 0; pos <= s.size() - oldVal.size();)
if (s[pos] == oldVal[0] && s.substr(pos, oldVal.size()) == oldVal)
s.replace(pos, oldVal.size(), newVal),
pos += newVal.size();
}
else
++pos;
}
}

int main()
{
string str{ "To drive straight thru is a foolish, tho courageous act." };
func(str, "tho", "though");
func(str, "thru", "through");
replace_with(str, "tho", "though");
replace_with(str, "thru", "through");
cout << str << endl;
return 0;
}

0 comments on commit a803715

Please sign in to comment.