Refactoring the legacy code which is using '§' char in the code I have a legacy code (might be 20 years old) as shown below. Where they are using the non ASCII '§' char in the code.
First they are preparing a SELECT query and the SELECT query looks like this...

SELECT 'ERROR_ID=§' || ERROR_ID ||  '§'  || ' AND ' ||  'DAT=:' || ...


They are using below code while preparing the SELECT query.
    strcat(query, "§" );    

From the SELECT query result which still contains the '§', they are doing comparision as shown below and if '§' char is present in the char array, they are replacing it with single quotes.
Since they are using the non ASCII char '§' in the code, I have been repoted an issue where the '§' is not getting replaced with single quotes and they
are appearing as question marks '?' in the log file. Below is the code where they are doing the replace

len_before=strlen(file_str);
k=l=end=0;
while(end==0)
{
    if(file_str[k] == '\'') 
    {
        strcpy( result,(char*)&amp;file_str[k]);
        file_str[k] = '\'';
        strcpy((char*)&amp;file_str[k+1],result);
        k++;
    }
    else
    {
        if((char)file_str[k]=='§') // Replace '§' by single quotes.
        {
            strncpy((char*)&amp;file_str[k],"'",1);
        }
    }
    k++;
    l++;
    if(l&gt;len_before)
        end=1;
};


So I am refactoring the code to not to use the non ASCII char '§' with sequence of chars as say ("!-!") for preparing the SELECT query as shown.

strcat(query, "!-!" );


And the result from the SELECT query I have to replace this sequence of chars ("!-!") with single quotes. Below is the refactored code...

string replaceAll(string inputString, string stringToFind, string stringToReplace)
{
    int found = inputString.find(stringToFind);
    while (found != string::npos)
    {
        inputString.replace(found, stringToFind.length(), stringToReplace);
        found = inputString.find(stringToFind, found + stringToReplace.length());
    }
    return inputString;
}

{
    // First replace every occurance of single quotes with double single quotes.
    string myString(file_str);
    myString = replaceAll(myString, "\'", "\'\'");

    // Replace the sequence of chars "!-!" with single quotes.
    myString = replaceAll(myString, "!-!", "\'");
}


Questions:


I would like to know if there are any pit falls which I need to look after?
Which sequence of chars can be used instead of "!-!"?
Is there any significane of char '§' (I believe that code is using '§' as a delimiter)in the code since they are connecting to the DB and fetching the data?
I don't see you using std::, surely you didn't put using namespace std somewhere in your code right?

I find your parameter names a bit confusing. stringToReplace is actually the replacement. The string you want to replace is stringToFind. Maybe use something like replaceWith or just replacement for the second parameter instead.

There is a type mismatch which your compiler should have caught (no warnings enabled?). The return value of find is size_t.

You can also pass the first argument as a reference and the other two as const reference.