fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main() {
  6. std::string original{"asdasd\"xdasdasd\"\"dasd\"asdasd"};
  7.  
  8. std::string start_tag = "<span class='yellow_code'>";
  9. std::string end_tag = "\"</span>";
  10.  
  11. std::size_t start_pos = 0;
  12. while((start_pos = original.find("\"", start_pos)) != std::string::npos)
  13. {
  14. std::size_t end_pos = original.find_first_of("\"", start_pos+1);
  15. if (end_pos == std::string::npos) // If the number of quotation characters is odd you would get undefined behavior
  16. break;
  17. std::size_t length = end_pos-start_pos;
  18. std::string originalWord = original.substr(start_pos+1, length-1);
  19. std::string newWord = start_tag + "\"" + originalWord + end_tag; // CHANGED: Added the start and end tags as std::string
  20. std::cout<<originalWord<<" : "<<newWord<<std::endl;
  21. original.replace(start_pos, length+1, newWord);
  22. start_pos = end_pos + start_tag.size() + end_tag.size(); // CHANGED: Added the size of the two tags
  23.  
  24. }
  25. return 0;
  26. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
xdasdasd : <span class='yellow_code'>"xdasdasd"</span>
dasd : <span class='yellow_code'>"dasd"</span>