Convert a string to a char array in C++
This post will discuss how to convert a string to a char array in C++.
1. Using strcpy function
The idea is to use the c_str() function to convert the std::string to a C-string. Then we can simply call the strcpy() function to copy the C-string into a char array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> #include <cstring> int main() { std::string s = "Hello World!"; char cstr[s.size() + 1]; strcpy(cstr, s.c_str()); // or, pass `&s[0]` std::cout << cstr << std::endl; return 0; } |
Output:
Hello World!
2. Using std::string::copy function
We know that the strcpy() is a C function. We can avoid using that in C++ by using std::string::copy instead.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { std::string s = "Hello World!"; char cstr[s.size() + 1]; s.copy(cstr, s.size() + 1); cstr[s.size()] = '\0'; std::cout << cstr << std::endl; return 0; } |
Output:
Hello World!
3. Using std::copy function
Finally, the <algorithm> header also provides the standard copy algorithm std::copy, which can be used in the following manner:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string s = "Hello World!"; char cstr[s.size() + 1]; std::copy(s.begin(), s.end(), cstr); cstr[s.size()] = '\0'; std::cout << cstr << std::endl; return 0; } |
Output:
Hello World!
That’s all about converting a string to a char array in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)