How to use string constructor In C++

The string constructor accepts char* as an argument and implicitly changes it to string.

C++




// C++ Program to demonstrate the conversion
// of char* to string using string constructor
#include <iostream>
using namespace std;
 
int main()
{
    const char* ch = "Welcome to w3wiki";
    string s(ch);
    cout << s;
    return 0;
}


Output

Welcome to w3wiki

Convert char* to string in C++

There are three ways to convert char* into string in C++.

  1. Using the “=” operator
  2. Using the string constructor
  3. Using the assign function

Similar Reads

1. Using the “=” operator

Using the assignment operator, each character of the char pointer array will get assigned to its corresponding index position in the string....

2. Using string constructor

...

3. Using the assign function

The string constructor accepts char* as an argument and implicitly changes it to string....