Inexpediency / cpp-practice

5 stars 0 forks source link

Замечания по Replace #5

Open alexey-malov opened 8 years ago

alexey-malov commented 8 years ago
alexey-malov commented 8 years ago
alexey-malov commented 8 years ago
alexey-malov commented 8 years ago
std::string FindAndReplace(const std::string & subject, const std::string & search, const std::string & replace)
{
    std::string result;
    std::string window = "";
    for (auto ch : subject)
    {
        window += ch;
        if (window.size() == search.size())
        {
            result += window == search ? replace : window;
            window = "";
        }
    }
    result += window;
    return result;
}
alexey-malov commented 8 years ago
    BOOST_AUTO_TEST_CASE(search_for_multi_char_string)
    {
        BOOST_CHECK_EQUAL(FindAndReplace("hello world", "world", "world-world"), "hello world-world");
    }