string replace() in C++

replace(): This function replaces a range of characters with a C string. 

This function returns a reference to this string

Parameters: Three parameters are required.


__i1 – Iterator referencing the start of the range to replace.

 __i2 – Iterator referencing end of the range to replace.

 __s – C string value to insert. 

Syntax:

str.replace(__i1, __i2, __s)

For Example:

str = "abc"

str.replace(str.begin(), str.begin()+1, "def") = > String becomes "defbc".

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str = "abc";

    str.replace(str.begin(), str.begin() + 1"def");

    cout << str << "\n";

    return 0;
}


No comments:

Post a Comment