Convert Array to String in C++
Convert Array to String in C++ (5 Programs).
1#include <iostream>2#include <string>3using namespace std;45int main() {6 char arr[] = {'H', 'e', 'l', 'l', 'o', '\0'};78 // Method 1: Direct assignment9 string str1 = arr;1011 // Method 2: Using string constructor12 string str2(arr);1314 // Method 3: Using assign()15 string str3;16 str3.assign(arr);1718 cout << "Array: " << arr << endl;19 cout << "String (method 1): " << str1 << endl;20 cout << "String (method 2): " << str2 << endl;21 cout << "String (method 3): " << str3 << endl;2223 return 0;24}
Output
Array: Hello String (method 1): Hello String (method 2): Hello String (method 3): Hello
What's going on
The program converts a character array (like {'H', 'e', 'l', 'l', 'o', '\0'}) into a C++ string (like "Hello"). C++ strings are more convenient than character arrays because they automatically manage memory, provide length information, and offer many useful methods. This conversion allows you to use modern C++ string features with C-style data.
Example:
Header Files Used
Declaring Variables
The program declares:
char arr[] = {'H', 'e', 'l', 'l', 'o', '\0'};
Method 1: Direct Assignment
string str1 = arr;
This is the simplest and most common method:
How it works:
Advantages:
Example:
char arr[] = "Hello";
string s = arr; // s = "Hello"
Method 2: Using String Constructor
string str2(arr);
This method explicitly uses the string constructor:
How it works:
Advantages:
Example:
char arr[] = "Hello";
string s(arr); // Explicit construction
Method 3: Using assign()
string str3;
str3.assign(arr);
This method uses the assign() member function:
How it works:
Advantages:
Example:
string s = "Old";
char arr[] = "New";
s.assign(arr); // s now contains "New"
Other Methods (Mentioned but not shown in code)
Method 4: Using append()
string str4;
str4.append(arr);
Example:
string s = "Prefix";
char arr[] = "Suffix";
s.append(arr); // s = "PrefixSuffix"
Method 5: Using stringstream
stringstream ss;
ss << arr;
string str5 = ss.str();
1#include <sstream>
Displaying Results
The program prints:
Output:
Array: Hello
String (method 1): Hello
String (method 2): Hello
String (method 3): Hello
All three methods produce the same string "Hello".
1cout << "Array: " << arr << endl;2cout << "String (method 1): " << str1 << endl;3cout << "String (method 2): " << str2 << endl;4cout << "String (method 3): " << str3 << endl;
Important Considerations
Null Terminator
:
Array vs Pointer
:
Memory Management
:
When to Use Each Method
-
Direct Assignment
: Best for most cases - simplest and most readable.
-
String Constructor
: When you want explicit construction or using in initialization lists.
-
assign()
: When replacing content of an existing string.
-
append()
: When adding array content to existing string.
-
stringstream
: When combining multiple values or complex formatting.
Best Practice
: Use direct assignment for simple conversions - it's clean and efficient.
Common Use Cases
Reading from C Functions
:
Many C functions return char*:
char* result = someCFunction();
string s = result; // Convert to C++ string
Working with Legacy Code
:
When interfacing with C libraries:
char buffer[100];
string data = buffer; // Convert to modern C++ string
1// ... fill buffer from C function
Building Strings
:
string s = "Start";
char addition[] = "End";
s.append(addition); // s = "StartEnd"
Safety Considerations
Null Terminator Required
:
Example of Problem
:
char arr[5] = {'H', 'e', 'l', 'l', 'o'}; // No null terminator!
string s = arr; // Undefined behavior - may read beyond array
Correct Way
:
char arr[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // With null terminator
string s = arr; // Safe
Or Use String Literal
:
char arr[] = "Hello"; // Automatically includes null terminator
string s = arr; // Safe
Performance Considerations
Copying
:
Memory
:
return 0;
This ends the program successfully.