string assign() in C++

assign(): It set (or assigns) value to the contents of a C string. It returns a reference to this string. 

This function is used to sets the value of this string to the value of __s. The data is copied, so there is no 

dependence on __s once the function returns.

Parameters: 

__s:– The C string to use.

Syntax:

str.assing(__s)

For Example:

str = ""

str.assign("def") => String becomes "def".

Approach

C++

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

int main()
{
    string str;
    str.assign("xyz");

    cout << str << "\n";

    return 0;
}


No comments:

Post a Comment