KatieP
KatieP

Reputation: 9

str.insert() doesn't work

I'm trying to search the string and insert a double quote. I'm searching a string for a delimiter and trying to insert "\"" before or after the delimiter (doesn't matter).

Program compiles, but it freezes when it reaches this function.It doesn't give me any errors while compiling.

What am I doing incorrectly?

string text="This|is|the|text";
string Quote="|";


void Insert()
{
  size_t i=0;
  string insert="\"";

  while ((i = text.find(Quote, i)) !=string::npos) 

{
    text.insert(i,insert);    
    }
 }

Upvotes: 0

Views: 229

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

Consider what's happening here. You find a '|' at position i, then insert a character at position i, then continue searching from position i. Where is the very next '|' now?

Upvotes: 3

Related Questions