-1

could something like this work? I've looked at guides and they don't seem to work for replacing a single char with two or more chars.

 for (int i = 0; i < s.length(); i++){ if(s[i] == '\"') s[i] = '\"\"'; } cout << s; 
4
  • Why don't you simply try? Commented Aug 22, 2017 at 20:28
  • 2
    Reminder: use '' for single characters. Use "" for multiple characters. Also, you can't assign multiple characters to a single character slot. Commented Aug 22, 2017 at 20:29
  • You could search the std::string section of your favorite references to see if there are any methods to help you insert characters into a string. Commented Aug 22, 2017 at 20:31
  • try to write the result into a new string; probably easier. Commented Aug 22, 2017 at 20:32

1 Answer 1

0

Your code will not perform as you expect.

You are telling std::string to assign a 2 character constant, '\"\"', into a slot that holds a single character.

Search the basic_string section of your favorite C++ reference to find a method to insert a string or multiple characters into a string.

Edit 1 - Example

std::string::size_type position = 0; position = s.find('"'); while (position != std::string::npos) { s.insert(position, "\""); position += 2; position = s.find('"'); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.