Hi,
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) 

    {
        text.replace(found,Quote.length(),doublequote);
        found += doublequote.length();
    }
       found=0;//I'm reading from a file and have multiple lines.
}
}

line 2 need escape characters

string text="I'm \"trying\" to search for quotes";

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

#include<iostream>
#include<string>
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';
}

Can you just use notepad search/replace to format the csv file? or is this just for practice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.