I am having trouble getting this function to work right. I would like it to search the string looking for quotations marks and insert a '\' right before the quotation. I have also tried using the string::find() function but it didn't work correctly either. Is there a better way? I am open to suggestions. Thank you.
testfile.txt
1 2
This" is a s"ample quo"tation sentence.
Thi"s is another "sample sentenc"e.
Just something I spotted, but why do you have line 41 where it is? Right now it performs that check at every iteration of the while loop, it's not a huge performance hit now but it bothers me a little and it's a bad habit.
What you have tried is good. but, small logic mistake in your code in find_qoutes().
your code with little changes to obtain your requirement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void find_quotes(string& this_phrase)
{
cout<<"\n"<<this_phrase<<endl;
int phrase_length=this_phrase.length();
for(int x=0; x<phrase_length;x++)
{
if(this_phrase[x]=='"')
{
this_phrase.insert(x++,"\\"); // x should be increment when new character added
cout<<this_phrase[x];
//Sleep(100);
}
// increment x for every loop is not required here. for will do that
}
cout<<"\n"<<this_phrase;
cin.get();
}
Wow! Thanks a lot for showing me how to fix my code. I didn't even think about trying that. A++ for the help. I have just got out of CS1 so I still have a lot to learn. Thanks again!