#include <IOSTREAM>
#include <STRING>
#include <CASSERT>
using namespace std;
string replaceAll(const string &str, const string &pattern, const string &replace)
{
string result = str;
string::size_type pos = 0;
string::size_type offset = 0;
while((pos = result.find(pattern, offset)) != string::npos)
{
result.replace(result.begin() + pos, result.begin() + pos + pattern.size(), replace);
offset = pos + replace.size();
}
return result;
}
int main()
{
assert(replaceAll("abcdefg", "bc", "de") == "adedefg");
assert(replaceAll("abc\nabc\nabc", "\n", "") == "abcabcabc");
assert(replaceAll("aaabbbaaa", "aaa", "1") == "1bbb1");
assert(replaceAll("ababab ababab", "ab", "1") == "111 111");
assert(replaceAll("abcdefg", "gf", "de") == "abcdefg");
assert(replaceAll("aaa", "a", "aa") == "aaaaaa");
assert(replaceAll("11aa11aa", "1", "11") == "1111aa1111aa");
return 0;
}