string function

closed account (EwCjE3v7)
I have no idea on how to do the following:
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".
Write a function that takes three strings s, oldVal and newVal
void foo(std::string& s, const std::string& oldVal, const std::string& newVal)

Using iterators, and the insert and erase functions replace all instances of oldVal that appear in s by newVal.
Use either search algorithm or find member function
http://en.cppreference.com/w/cpp/algorithm/search
http://en.cppreference.com/w/cpp/string/basic_string/find

Use erase to delete portion to replace and insert to add new portion
http://en.cppreference.com/w/cpp/string/basic_string/erase
http://en.cppreference.com/w/cpp/string/basic_string/insert

It would be easier to use replace, but your assigment requires you to use inser and erase
http://en.cppreference.com/w/cpp/string/basic_string/replace
Topic archived. No new replies allowed.