C++, best way to change a string at a particular index

前端 未结 4 983
余生分开走
余生分开走 2020-12-24 13:02

I want to change a C++ string at a particular index like this:

string s = \"abc\";
s[1] = \'a\';

Is the following code valid? Is this an a

4条回答
  •  天涯浪人
    2020-12-24 13:20

    Yes the code you have written is valid. You can also try:

    string num;
    cin>>num;
    num.at(1)='a';
    cout<

    the std::replace can also be used to replace the charecter. Here is the reference link http://www.cplusplus.com/reference/string/string/replace/

    Hope this helps.

提交回复
热议问题