By Safa Mulani
In this article, we will be focusing on the different ways to convert String to char array and char array to String in C++. While dealing with String data, we may need to convert the string data items to character array and vice-versa. This tutorial will help you solve exactly that.
C++ provides us with the following techniques to convert a string to char array:
C++ c_str()
function along with C++ String strcpy()
function can be used to convert a string to char array easily.
The c_str() method represents the sequence of characters in an array of string followed by a null character (‘\0’). It returns a null pointer to the string.
Syntax:
string-name.c_str();
Example:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "";
cout<<"Enter the string:\n";
cin>>str;
char arr[str.length() + 1];
strcpy(arr, str.c_str());
cout<<"String to char array conversion:\n";
for (int i = 0; i < str.length(); i++)
cout << arr[i];
return 0;
}
Output:
Enter the string:
JournalDev
String to char array conversion:
JournalDev
For the conversion of char array to a string, we can use C++ for loops with ease.
Example:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "";
cout<<"Enter the string:\n";
cin>>str;
char arr[str.length() + 1];
cout<<"String to char array conversion:\n";
for (int x = 0; x < sizeof(arr); x++) {
arr[x] = str[x];
cout << arr[x];
}
return 0;
}
Output:
Enter the string:
JournalDev
String to char array conversion:
JournalDev
The mentioned techniques can be used to convert a char array to string in C++:
C++ provides us with '+' operator
to concatenate or add data items to a variable.
Example:
#include <bits/stdc++.h>
using namespace std;
int main()
{
char arr[] = { 'J', 'O', 'U', 'R', 'N', 'A', 'L', 'D', 'E', 'V' };
int size_arr = sizeof(arr) / sizeof(char);
string str = "";
for (int x = 0; x < size_arr; x++) {
str = str + arr[x];
}
cout<<"Converted char array to string:\n";
cout << str << endl;
return 0;
}
Output:
Converted char array to string:
JOURNALDEV
C++ has got the concept of overloading which makes an operator perform other operations apart from the basic or default operation.
'=' operator
to store the data items character by character into the newly created empty string.Example:
#include <bits/stdc++.h>
using namespace std;
int main()
{
char arr[] = { 'J', 'O', 'U', 'R', 'N', 'A', 'L', 'D', 'E', 'V' };
int size_arr = sizeof(arr) / sizeof(char);
string str = "";
str = arr;
cout<<"Converted char array to string:\n";
cout << str << endl;
return 0;
}
Output:
Converted char array to string:
JOURNALDEV
In the context of conversion of char array to string, we can use C++ String Constructor for the same.
Syntax:
string string-name(char array-name);
This constructor takes a sequence of characters terminated by a null character as an input parameter.
Note: This constructor string string()
can be used only at the time of string declaration throughout the program.
Example:
#include <bits/stdc++.h>
using namespace std;
int main()
{
char arr[] = { 'J', 'O', 'U', 'R', 'N', 'A', 'L', 'D', 'E', 'V' };
int size_arr = sizeof(arr) / sizeof(char);
string str(arr);
cout<<"Converted char array to string:\n";
cout <<str<< endl;
return 0;
}
Output:
Converted char array to string:
JOURNALDEV
In this article, we have understood various techniques to convert string to char array and vice-versa in C++.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Example (1) doesnt compile with visual studio 2019. Error type: error C2131: expression did not evaluate to a constant 1>C:\Users\tmcla\source\repos\ConsoleApplication35\ConsoleApplication35.cpp(12,24): message : failure was caused by call of undefined function or one not declared ‘constexpr’ 1>C:\Users\tmcla\source\repos\ConsoleApplication35\ConsoleApplication35.cpp(12,24): message : see usage of ‘std::basic_string<char,std::char_traits,std::allocator>::length’ must have a constant value. How can I rewrite this to a successful compile? Would a vector construction work better?
- Terrence McLaughlin
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.