Convert char* to std::string in C++
Strings are generally represented as an instance of std::string class in C++. But the language also supports the older C-Style representation where they are represented as array of characters (char* or char[]) terminated by null character '\0'. In this article, we will learn how to convert the char* into std::string in C++.
Following are the 5 different ways to convert char* to std::string in C++:
Table of Content
Using Assignment Operator (=)
In C++, the easiest method to convert char* (C-style string) to a std::string is by simply assigning it to the std::string object using (=) assignment operator.
Example
// C++ Program to illsutrate the conversion
// of char* to string using =
#include <bits/stdc++.h>
using namespace std;
int main() {
const char *cstr = "Welcome to GeeksForGeeks";
// Conversion of char* to string using =
string s = cstr;
cout << s;
return 0;
}
// C++ Program to illsutrate the conversion
// of char* to string using =
using namespace std;
int main() {
const char *cstr = "Welcome to GeeksForGeeks";
// Conversion of char* to string using =
string s = cstr;
cout << s;
return 0;
}
Output
Welcome to GeeksForGeeks
Using std::string Constructor
We can also create an instance of std::string using the char*
(C-style string) by passing it to the std::string constructor at the time of its declaration.
Example
// C++ Program to illustrate the conversion
// of char* to string using string constructor
#include <bits/stdc++.h>
using namespace std;
int main() {
const char *cstr = "Welcome to GeeksForGeeks";
// Conversion of char* to string using
// string constructor
string s(cstr);
cout << s;
return 0;
}
// C++ Program to illustrate the conversion
// of char* to string using string constructor
using namespace std;
int main() {
const char *cstr = "Welcome to GeeksForGeeks";
// Conversion of char* to string using
// string constructor
string s(cstr);
cout << s;
return 0;
}
Output
Welcome to GeeksForGeeks
Using string::assign() Method
Apart from the above methods, we can also convert the char* (character array) to std::string using std::assign() method.
Syntax
str.assign(first, last);
where first and last the pointer to the first character and the character after the last character of char* string (not considering the null character).
Example
// C++ Program to illustrate the conversion of
// char* to std::string using string::assign()
#include <bits/stdc++.h>
using namespace std;
int main() {
const char *cstr = "Welcome to GeeksForGeeks";
int n = strlen(cstr);
// Create an empty std::string object
string str;
// Conversion of char* to string using
// assign function
str.assign(cstr, cstr + n);
cout << str;
return 0;
}
// C++ Program to illustrate the conversion of
// char* to std::string using string::assign()
using namespace std;
int main() {
const char *cstr = "Welcome to GeeksForGeeks";
int n = strlen(cstr);
// Create an empty std::string object
string str;
// Conversion of char* to string using
// assign function
str.assign(cstr, cstr + n);
cout << str;
return 0;
}
Output
Welcome to GeeksForGeeks
Using std::copy with std::back_inserter()
C++ STL provides the std::copy() method that works well for both iterators and pointers. So, we can use this method to copy the content of char* string to std::string. We may need to use std::back_inserter() method for copying in std::string as std::copy() method does not allocate new space in the destination container.
Example
// C++ Program to convert char* to string
// using std::copy() and std::back_inserter()
#include <bits/stdc++.h>
using namespace std;
int main() {
const char *cstr = "Welcome to GeeksForGeeks";
int n = strlen(cstr);
// Create an empty std::string object
string str;
// Copy characters from char* to string using
// std::copy and std::back_inserter
copy(cstr, cstr + n, back_inserter(str));
cout << str;
return 0;
}
// C++ Program to convert char* to string
// using std::copy() and std::back_inserter()
using namespace std;
int main() {
const char *cstr = "Welcome to GeeksForGeeks";
int n = strlen(cstr);
// Create an empty std::string object
string str;
// Copy characters from char* to string using
// std::copy and std::back_inserter
copy(cstr, cstr + n, back_inserter(str));
cout << str;
return 0;
}
Output
Welcome to GeeksForGeeks
Manually Using string::push_back()
It is the manual method to convert char* to std::string by pushing all the characters of the char* one by one to the std::string object.
Example
// C++ Program to convert char* to string
// by pushing characters one by one
#include <bits/stdc++.h>
using namespace std;
int main() {
const char *cstr = "Welcome to GeeksForGeeks";
// Create an empty string
string str;
// Manually push characters from char* to string
for (int i = 0; cstr[i] != '\0'; i++) {
str.push_back(cstr[i]);
}
cout << str << endl;
return 0;
}
// C++ Program to convert char* to string
// by pushing characters one by one
using namespace std;
int main() {
const char *cstr = "Welcome to GeeksForGeeks";
// Create an empty string
string str;
// Manually push characters from char* to string
for (int i = 0; cstr[i] != '\0'; i++) {
str.push_back(cstr[i]);
}
cout << str << endl;
return 0;
}
Output
Welcome to GeeksForGeeks