I'm relatively new to C++. I'm trying to search for a double quote and replace it with 2 double quotes ( for CSV format conversion). The code compiles and works fine when Quote is any string. But it freezes when I try to search for a "\"".
Help appreciated.

string Quote="\"";//Maybe I initialize this wrong?
string text="I'm "trying" to search for quotes";

void SearchforQuote()

size_t found=0;
string doublequote="\"\"";
for(int i=0;i<text.size();i++)
       while ((found = text.find(Quote,found)) !=string::npos) 

        found += doublequote.length();
       found=0;//I'm reading from a file and have multiple lines.

Edited 2 Years Ago by michelle.trinsky

In reality, I'm reading from a .txt file. So I won't be able to escape quotes the way you suggest. Is there any other way?

In that case the escape characters are not needed -- only useful for literal strings.

You don't need the for loop. This works

using namespace std;

string Quote = "\"";//Maybe I initialize this wrong?
string text = "I'm \"trying\" to search for quotes";

int main()

    size_t found = 0;
    string doublequote = "\"\"";
        while ((found = text.find(Quote, found)) != string::npos)

            text.replace(found, Quote.length(), doublequote);
            found += doublequote.length();
    cout << text << '\n';
This article has been dead for over six months. Start a new discussion instead.