Create a character array from a string in C++
This post will discuss how to create a character array from a string in C++.
1. Using std::string::copy
The standard solution to copy a sequence of characters from a string to a character array in C++ is with std::string::copy. Here’s how the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string s = "Hello"; char chars[s.length() + 1]; s.copy(chars, s.length() + 1); std::cout << chars << std::endl; return 0; } |
Output:
Hello
Or dynamically create a character array of size n+1 for a string of length n and include null-terminating character to be safe, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string s = "Hello"; char* chars = new char[s.length() + 1]; s.copy(chars, s.length()); chars[s.length()] = '\0'; std::cout << chars << std::endl; return 0; } |
Output:
Hello
2. Using strcpy() function
The strcpy() function is used to copy the specified string to the destination buffer, including the null-terminating character. The buffer should be long enough to contain all characters of the C string and a null-terminating character.
This logic would translate to the following code. Since strcpy() function accepts a C string, the c_str() member function of std::string is used to get a C string from a C++ string.
|
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"; char chars[s.length() + 1]; strcpy(chars, s.c_str()); std::cout << chars << std::endl; return 0; } |
Output:
Hello
Note that the strcpy() is a C function and should be avoided in C++. We can also dynamically create a character array of size n+1 for a string of length n, as shown below:
|
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"; char* chars = new char[s.length() + 1]; strcpy(chars, s.c_str()); std::cout << chars << std::endl; return 0; } |
Output:
Hello
Alternatively, we can use the strncpy() function to copy all characters of the string to the destination buffer and null-terminate the string explicitly.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> #include <cstring> int main() { std::string s = "Hello"; char chars[s.length() + 1]; strncpy(chars, s.c_str(), sizeof(chars)); chars[sizeof(chars) - 1] = '\0'; std::cout << chars << std::endl; return 0; } |
Output:
Hello
3. Using c_str() function
To get a null-terminated const array of characters representing the string content, we can directly use the c_str() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <string> int main() { std::string s = "Hello"; const char *chars = s.c_str(); std::cout << chars << std::endl; return 0; } |
Output:
Hello
To get a char* instead of const char*, do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <string> int main() { std::string s = "Hello"; char* chars = &s[0]; std::cout << chars << std::endl; return 0; } |
Output:
Hello
That’s all about creating a character array from a string 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 :)