#include <iostream>
#include <string>
using namespace std;
int main() {
std::string original{"asdasd\"xdasdasd\"\"dasd\"asdasd"};
std::string start_tag = "<span class='yellow_code'>";
std::string end_tag = "\"</span>";
std::size_t start_pos = 0;
while((start_pos = original.find("\"", start_pos)) != std::string::npos)
{
std::size_t end_pos = original.find_first_of("\"", start_pos+1);
if (end_pos == std::string::npos) // If the number of quotation characters is odd you would get undefined behavior
break;
std::size_t length = end_pos-start_pos;
std::string originalWord = original.substr(start_pos+1, length-1);
std::string newWord = start_tag + "\"" + originalWord + end_tag; // CHANGED: Added the start and end tags as std::string
std::cout<<originalWord<<" : "<<newWord<<std::endl;
original.replace(start_pos, length+1, newWord);
start_pos = end_pos + start_tag.size() + end_tag.size(); // CHANGED: Added the size of the two tags
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CglzdGQ6OnN0cmluZyBvcmlnaW5hbHsiYXNkYXNkXCJ4ZGFzZGFzZFwiXCJkYXNkXCJhc2Rhc2QifTsKCQogICAgc3RkOjpzdHJpbmcgc3RhcnRfdGFnID0gIjxzcGFuIGNsYXNzPSd5ZWxsb3dfY29kZSc+IjsKICAgIHN0ZDo6c3RyaW5nIGVuZF90YWcgPSAiXCI8L3NwYW4+IjsKCiAgICBzdGQ6OnNpemVfdCBzdGFydF9wb3MgPSAwOwogICAgd2hpbGUoKHN0YXJ0X3BvcyA9IG9yaWdpbmFsLmZpbmQoIlwiIiwgc3RhcnRfcG9zKSkgIT0gc3RkOjpzdHJpbmc6Om5wb3MpCiAgICB7CiAgICAgICAgc3RkOjpzaXplX3QgZW5kX3BvcyA9IG9yaWdpbmFsLmZpbmRfZmlyc3Rfb2YoIlwiIiwgc3RhcnRfcG9zKzEpOwogICAgICAgIGlmIChlbmRfcG9zID09IHN0ZDo6c3RyaW5nOjpucG9zKSAvLyBJZiB0aGUgbnVtYmVyIG9mI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