0

I am trying to write a REPLACE function that will replace the given string by the requiredstring. When I am dry running the function on paper, everything seems to be fine but while executing, it's not giving the correct output. The code is as follows:-

string REPLACE(string src,string reqd,string given)
{
    int i,j,k;
    int pos = FIND(src,given);
    if(pos==-1)
        return "";
    else
    {
        char *arr = new char[src.length()+reqd.length()-given.length()];  // creating the array that will hold the modified string
        for(i=0;i<pos;i++)
            arr[i] = src[i];     // copying the initial part of the string
        for(i=pos,j=0;i<pos+reqd.length()+1&&j<reqd.length();i++,j++)
            arr[i] = reqd[j];    // copying the required string into array
        for(i=pos+reqd.length()+1,k=0;i<sizeof(arr);i++,k++)
            arr[i] = src[pos+given.length()+k];   // copying the remaining part of source string into the array
        return arr;
    }   
}

Here the FIND is also written by me and has been tested in many cases. I don't see any error in FIND.

4
  • Why are you manually allocating memory? Isn't std:string good enough? You also leak memory every time you call this function. Commented Sep 2, 2013 at 15:22
  • But then, I cannot make the string flexible enough to handle modifications that change their lengths. Commented Sep 2, 2013 at 15:27
  • 1
    Yes you can, use std::string arr(src.length()+reqd.length()-given.length(), 0);. Commented Sep 2, 2013 at 15:28
  • Sure you can,std::string is a dynamic container that can change its size. Commented Sep 2, 2013 at 15:29

3 Answers 3

1
for(i=pos+reqd.length()+1,k=0; i<sizeof(arr); i++,k++)
//                               ^^^^^^^^^^^
//                           This is always the same

sizeof(arr) is the same as sizeof(char*), a compile-time constant value. You need to keep the size of a dynamically allocated array yourself (or, better yet, simply use std::string instead).

Sign up to request clarification or add additional context in comments.

1 Comment

Works fine if I replace sizeof(arr) by src.length()+reqd.length()-given.length()+1
1

I don't think it is a good idea to mix std::string with char arrays. Following should work:

string REPLACE(string src,string reqd,string given)
{
    int pos = FIND(src,given);

    src.replace( pos, given.size(), reqd );
    return src;    
}

2 Comments

Thanks for the advice. Could you please elaborate on why using char arrays and string together is a bad idea?
The class std::string itself can be used like a char array if required. Other than that, as already shown, it provides a lot more functionality and clarity to your code. It also automatically takes care of dynamic memory allocation without the fear of memory leaks.
0

Assuming you do not want to re-invent the wheel:

string REPLACE(string src,string reqd,string given)
{
    string str(src);
    size_t pos = str.find(given);
    if(pos == std::string::npos)
        return "";
    str.replace(pos, given.length(), reqd);
    return str;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.