I tried to solve this simple problem using a string::iterator. The solution works fine on my computer, but it gets a runtime error when I submit it to UVa-onlinejudge platform. I tried another solution using array index and got accepted. I looked and couldn’t figure out why the solution using string::iterator was generating runtime error. I’d appreciate it if someone could point what I did wrong. Thank you.
//***Solution using iterator
while(getline(cin,input)){
string::const_iterator iter = input.begin();
while (iter!=input.end()){
if(*iter=='"'){
input.erase(iter);
if(firstQuote){
input.insert(iter,2,'`');
firstQuote = false;
}else{
input.insert(iter,2,'\'');
firstQuote = true;
}
}
iter++;
}
// Output:
cout << input << endl;
}