In this article, we will discuss different ways to convert a string to a char array in C++.
Table Of Contents
Problem Description
We have given a string and we have to convert the string to a char array.
Input
string s = "Hello World";
Output
Frequently Asked:
Hello World
We can convert a string to a char array using different techniques. Let’s discuss all these methods one by one.
Convert string to char array using strcpy() function
In this approach, the string is copied into the char array with the help of strcpy() function.
Time Complexity: O(n)
Space Complexity: O(1)
#include <iostream> #include <cstring> using namespace std; int main() { // declare the string string strValue = "Hello World"; // declare the char array with capacity // one more than the string size char chArr[strValue.size() + 1]; // apply strcpy function for copy // the string in char array strcpy(chArr, strValue.c_str()); // print the char array cout << chArr << endl; return 0; }
Output
Best Resources to Learn C++:
Hello World
In this example, we created a char array from a string object in C++.
Convert string to char array using string::copy() function
In this approach, the string is copied in the char array with the help of copy() member function of string class.
Time Complexity: O(n)
Space Complexity: O(1)
#include <iostream> using namespace std; int main() { // declare the string string s = "Hello World"; // declare the char array with capacity // one more than the string size char cstr[s.size() + 1]; // apply string::copy function for copy // the string in char array s.copy(cstr, s.size() + 1); // assign '#include <iostream> using namespace std; int main() { // declare the string string s = "Hello World"; // declare the char array with capacity // one more than the string size char cstr[s.size() + 1]; // apply string::copy function for copy // the string in char array s.copy(cstr, s.size() + 1); // assign '\0' at the end of char array cstr[s.size()] = '\0'; // print the char array cout << cstr <<endl; return 0; }' at the end of char array cstr[s.size()] = '#include <iostream> using namespace std; int main() { // declare the string string s = "Hello World"; // declare the char array with capacity // one more than the string size char cstr[s.size() + 1]; // apply string::copy function for copy // the string in char array s.copy(cstr, s.size() + 1); // assign '\0' at the end of char array cstr[s.size()] = '\0'; // print the char array cout << cstr <<endl; return 0; }'; // print the char array cout << cstr <<endl; return 0; }
Output
Hello World
In this example, we created a char array from a string object in C++.
Convert string to char array using STL’s copy() function
In this approach, the string is copied in the char array with the help of copy() function of STL.
Time Complexity: O(n)
Space Complexity: O(1)
#include <iostream> #include <algorithm> using namespace std; int main() { // declare the string string s = "Hello World"; // declare the char array with capacity // one more than the string size char cstr[s.size() + 1]; // apply copy function for copy the string // in char array copy(s.begin(), s.end(), cstr); // assign '#include <iostream> #include <algorithm> using namespace std; int main() { // declare the string string s = "Hello World"; // declare the char array with capacity // one more than the string size char cstr[s.size() + 1]; // apply copy function for copy the string // in char array copy(s.begin(), s.end(), cstr); // assign '\0' at the end of char array cstr[s.size()] = '\0'; // print the char array cout << cstr << endl; return 0; }' at the end of char array cstr[s.size()] = '#include <iostream> #include <algorithm> using namespace std; int main() { // declare the string string s = "Hello World"; // declare the char array with capacity // one more than the string size char cstr[s.size() + 1]; // apply copy function for copy the string // in char array copy(s.begin(), s.end(), cstr); // assign '\0' at the end of char array cstr[s.size()] = '\0'; // print the char array cout << cstr << endl; return 0; }'; // print the char array cout << cstr << endl; return 0; }
Output
Hello World
In this example, we created a char array from a string object in C++.
Summary
In this article, we have seen various methods for converting a string to a char array with the use of STL functions and string’s member functions. Each method has its own time and space complexity. Happy Learning.