How to insert a character every N characters in a string in C++

后端 未结 5 984
说谎
说谎 2021-01-14 05:33

How can I insert a character into a string exactly after 1 character?

I need to insert \'|\' into the string after every other char

5条回答
  •  孤城傲影
    2021-01-14 06:24

    You can use

    string& insert (size_t pos, const string& str);
    

    You would have to loop through the string, inserting a character each time.

    for (int i = 1; i < str.size(); i++) {
          str << str.insert(i, '|');
          i++;
    }
    

提交回复
热议问题