Thread: Comparing strings problem.

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40

    Comparing strings problem.

    Hello!

    So I have a problem with finishing this task from C++ Primer 5th edi, Chapter 9:

    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”.

    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? 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.

    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?

    Code:
    string abbrev(string s, string oldVal, string newVal)
    {
        string word, newString;
        istringstream val(s);
        while (val >> word)
        {
            if (word == oldVal)
                word = newVal;
            newString += word + " ";
        }
        return newString;
    }
    This is the function I have written, just to show some work.
    "Derp, derp, derp" - Me

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your function won't work because if I specify abbrev("Hello World", "Hel", "Bel"), it won't change anything.
    Unfortunately, to my knowledge, there are no algorithms in the standard library that allows you to search a range where both the input what to search and the token to find are both given as iterators, so you may have to do it the old-fashioned way.
    An iterator represents a character in a string, so given that you can iterate through each character in a string, given some input i and something to find f, how would you go about finding this f in i?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.

    Quote 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".

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help comparing strings
    By busdude in forum C Programming
    Replies: 2
    Last Post: 12-02-2009, 09:00 PM
  2. Replies: 4
    Last Post: 03-18-2009, 05:01 PM
  3. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  4. Comparing 2 Strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2002, 07:01 PM
  5. problem comparing strings with strcmp
    By mikefen in forum C Programming
    Replies: 2
    Last Post: 12-03-2001, 10:45 PM