Convert char* to std::string in C++
Last Updated :
16 Oct, 2024
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++:
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++
// 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;
}
OutputWelcome 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++
// 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;
}
OutputWelcome 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++
// 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;
}
OutputWelcome 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++
// 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;
}
OutputWelcome 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++
// 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;
}
OutputWelcome to GeeksForGeeks
Similar Reads
How to Convert a std::string to char* in C++? In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor
1 min read
Convert String to Char Array in C++ In C++, we usually represent text data using the std::string object. But in some cases, we may need to convert a std::string to a character array, the traditional C-style strings. In this article, we will learn how to convert the string to char array in C++.ExamplesInput: str = "geeksforgeeks"Output
4 min read
Convert character array to string in C++ This article shows how to convert a character array to a string in C++. The std::string in c++ has a lot of inbuilt functions which makes implementation much easier than handling a character array. Hence, it would often be easier to work if we convert a character array to string. Examples: Input: ch
4 min read
Convert Vector of Characters to String in C++ In this article, we will learn different methods to convert the vector of character to string in C++.The most efficient method to convert the vector of characters to string is by using string's range constructor. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std;
2 min read
Convert Float to String In C++ In this article, we learn how we can convert float to string in C++ using different methods: Using the to_string()Using stringstreamUsing MacrosUsing lexical_cast from the boost library1. Using to_string() The to_string() method takes a single integer variable or other data type and converts it into
3 min read