Convert String to Array in C++
Convert String To Array in C++ (5 Programs).
1#include <iostream>2#include <string>3#include <vector>4#include <cstring>5using namespace std;67int main() {8 string str = "Hello";910 // Method 1: Using c_str() and copying11 char arr1[10];12 strcpy(arr1, str.c_str());1314 // Method 2: Using vector of chars15 vector<char> arr2(str.begin(), str.end());1617 // Method 3: Manual copying18 char arr3[10];19 for (int i = 0; i < str.length(); i++) {20 arr3[i] = str[i];21 }22 arr3[str.length()] = '\0';2324 cout << "String: " << str << endl;25 cout << "Array (method 1): " << arr1 << endl;2627 return 0;28}
Output
String: Hello Array (method 1): Hello
What's going on
The program converts a C++ string (like "Hello") into a character array (like {'H', 'e', 'l', 'l', 'o', '\0'}). C++ strings are dynamic and managed, while character arrays are fixed-size and require manual memory management. This conversion bridges the gap between modern C++ strings and C-style character arrays.
Example:
Header Files Used
Declaring Variables
The program declares:
string str = "Hello";
Method 1: Using c_str() and strcpy()
char arr1[10];
strcpy(arr1, str.c_str());
This is a common C-style method:
How it works:
Advantages:
Disadvantages:
Important: The array size (10) must be at least str.length() + 1 to accommodate the null terminator.
Method 2: Using Vector
vector<char> arr2(str.begin(), str.end());
This method uses a C++ vector:
How it works:
Advantages:
Disadvantages:
Example:
vector<char> vec(str.begin(), str.end());
1// Access: vec[0], vec[1], etc.2// Size: vec.size()
Method 3: Manual Copying
char arr3[10];
for (int i = 0; i < str.length(); i++) {
arr3[i] = str[i];
}
arr3[str.length()] = '\0';
This method manually copies each character:
How it works:
Advantages:
Disadvantages:
Other Methods (Mentioned but not shown in code)
Method 4: Using copy() Algorithm
char arr4[10];
copy(str.begin(), str.end(), arr4);
arr4[str.length()] = '\0';
1#include <algorithm>
Method 5: Using string data() Method
char* arr5 = new char[str.length() + 1];
strcpy(arr5, str.data());
1// Remember to delete[] arr5 when done
Displaying Results
The program prints:
Output:
String: Hello
Array (method 1): Hello
The array is printed as a C-string, which automatically stops at the null terminator.
1cout << "String: " << str << endl;2cout << "Array (method 1): " << arr1 << endl;
Important Considerations
Null Terminator
:
Buffer Size
:
Memory Management
:
When to Use Each Method
-
c_str() + strcpy()
: When you need a fixed-size C-style array and know the maximum size.
-
Vector
: When you need dynamic size or want automatic memory management (recommended).
-
Manual Copying
: When you need custom logic during copying or want to understand the process.
-
copy() Algorithm
: When you prefer STL algorithms and want clean code.
-
data() Method
: When you need direct access to string's internal buffer (advanced use).
Best Practice
: Use vector for most cases - it's safe and modern. Use fixed arrays only when size is known and fixed.
Safety Considerations
Buffer Overflow Prevention
:
Example with Safety Check
:
const int MAX_SIZE = 10;
if (str.length() < MAX_SIZE) {
strcpy(arr, str.c_str());
} else {
}
1// Handle error - string too long
Modern Alternative
:
Accessing Array Elements
After conversion, access characters like a normal array:
char arr[10];
strcpy(arr, str.c_str());
char first = arr[0]; // 'H'
char second = arr[1]; // 'e'
// Iterate through array
for (int i = 0; arr[i] != '\0'; i++) {
cout << arr[i];
}
1// Access individual characters
return 0;
This ends the program successfully.