Originally Posted by
Iceboxes
if I know the string s, it would be rather easy to do the task, because then I would know the positions of where to insert and erase right?
Yes, and you do indeed "know" the string s since it is an argument to the function.
Originally Posted by
Iceboxes
Problem is, the task isnt specified, and I had a lot of trouble figuring out a way of comparing two ranges of iterators or comparing two words and etc etc.
The task appears to be specified: "replace all instances of oldVal that appear in s by newVal".
Originally Posted by
Iceboxes
I have made a function that works, but it uses stringstreams and no iterators or erase and insert functions... Does anyone know a good way of doing the task?
Start by observing what is wrong with your function. Your instructions recommend: 'Test your function by using it to replace common abbreviations, such as "tho" by "though" and "thru" by "through".' This should be easy, e.g.,
Code:
int main()
{
string text = "tho I did not study, I pulled thru.";
text = abbrev(text, "tho", "though");
text = abbrev(text, "thru", "through");
cout << text << endl;
}
Observe that the result using your implementationm of abbrev is:
Code:
though I did not study, I pulled thru.
Obviously, "thru" was not replaced by "through" because it was parsed as "thru.". Furthermore, the spaces after the comma were not retained, which might not be a problem, but technically violates the requirements.
You can find the positions of the search string by using the find member function of std::string, though that is index-based rather than iterator based.