Convert Array to String
Convert Array to String in C++ (5 Programs)
C++ Convert Array to String Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <string>
using namespace std;
int main() {
char arr[] = {'H', 'e', 'l', 'l', 'o', '\0'};
// Method 1: Direct assignment
string str1 = arr;
// Method 2: Using string constructor
string str2(arr);
// Method 3: Using assign()
string str3;
str3.assign(arr);
cout << "Array: " << arr << endl;
cout << "String (method 1): " << str1 << endl;
cout << "String (method 2): " << str2 << endl;
cout << "String (method 3): " << str3 << endl;
return 0;
}Array: Hello String (method 1): Hello String (method 2): Hello String (method 3): Hello
Understanding Convert Array to String
This program teaches you how to convert a character array (C-style string) into a C++ string. This conversion is very common and usually straightforward because C++ strings can easily accept character arrays. Understanding different conversion methods helps you write clean, efficient code and handle various scenarios when working with legacy C code or character arrays.
---
1. What This Program Does
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:
---
2. Header Files Used
---
3. Declaring Variables
The program declares:
char arr[] = {'H', 'e', 'l', 'l', 'o', '\0'};
---
4. 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"
---
5. 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
---
6. 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"
---
7. 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();
---
#include <sstream>8. Displaying Results
The program prints:
Output:
All three methods produce the same string "Hello".
---
cout << "Array: " << arr << endl;
cout << "String (method 1): " << str1 << endl;
cout << "String (method 2): " << str2 << endl;
cout << "String (method 3): " << str3 << endl;9. Important Considerations
Null Terminator
:
Array vs Pointer
:
Memory Management
:
---
10. 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.
---
11. 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
// ... fill buffer from C functionBuilding Strings
:
string s = "Start";
char addition[] = "End";
s.append(addition); // s = "StartEnd"
---
12. 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
---
13. Performance Considerations
Copying
:
Memory
:
---
14. return 0;
This ends the program successfully.
---
Summary
This program is essential for beginners learning how to work with both C-style character arrays and modern C++ strings, understanding the relationship between them, and choosing the right conversion method for different scenarios in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Convert Array to String
This C++ program is part of the "String Conversion Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.