Skip to content

Dexter

Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #191362
    Dexter
    Member

    The basic has a function called “Replace” which takes 3 strings and does as i described. I looked at the string::replace but none of its forms can take the 3 stings and do the job. If i use a position and size then surely if the replacer is longer than the replace-ee then thats going to overwrite more than i want it too :/ Plus im not sure how to even get the position or length i need either

    #include
    #include
    #include
    #include
    
    using namespace std;
    
    string Replace(string a,string b,string c)
    {
    int pos = a.find(b);
    a.replace(pos, c.length(), c);
    
    return a;
    }
    
    int main()
    
    {
    std::cout << Replace(" Hello World","Hello","goodbye");
    }

    This is my attempt but i am getting
    goodbyeorld

    As its simply overwriting rather than resizing a.

    edit:
    Ah ok fixed it, i needed to supply the length of b. Now i just need to make it parse the whole string 🙂

    Is this the best way of doing this? by (dman (20) )

Viewing 1 post (of 1 total)