0
0
CppHow-ToBeginner · 2 min read

C++ How to Convert Char Array to String Easily

You can convert a char array to a string in C++ by using the std::string constructor like std::string str(charArray); where charArray is your char array.
📋

Examples

Inputchar arr[] = {'H','i','\0'};
OutputHi
Inputchar arr[] = "Hello World";
OutputHello World
Inputchar arr[] = { '\0' };
Output
🧠

How to Think About It

To convert a char array to a string, think of the char array as a list of letters ending with a special end marker \0. The string constructor reads these letters until it finds this end marker and creates a string from them.
📐

Algorithm

1
Take the char array as input.
2
Use the string constructor to create a string from the char array.
3
Return or use the created string.
💻

Code

cpp
#include <iostream>
#include <string>

int main() {
    char charArray[] = "Hello C++";
    std::string str(charArray);
    std::cout << str << std::endl;
    return 0;
}
Output
Hello C++
🔍

Dry Run

Let's trace converting charArray = "Hello C++" to string

1

Input char array

charArray = {'H','e','l','l','o',' ','C','+','+','\0'}

2

Create string from char array

std::string str(charArray) reads characters until '\0'

3

Output string

str = "Hello C++"

StepCharacter ReadString So Far
1HH
2eHe
3lHel
4lHell
5oHello
6 Hello
7CHello C
8+Hello C+
9+Hello C++
10\0Stop reading
💡

Why This Works

Step 1: String constructor reads char array

The std::string constructor takes the char array and reads characters one by one.

Step 2: Stops at null character

It stops reading when it finds the null character \0, which marks the end of the char array.

Step 3: Creates string object

It then creates a string object containing all characters read before the null character.

🔄

Alternative Approaches

Using assignment operator
cpp
#include <iostream>
#include <string>

int main() {
    char charArray[] = "Example";
    std::string str = charArray;
    std::cout << str << std::endl;
    return 0;
}
This is simpler syntax but does the same as the constructor.
Using std::string::assign() method
cpp
#include <iostream>
#include <string>

int main() {
    char charArray[] = "Assign method";
    std::string str;
    str.assign(charArray);
    std::cout << str << std::endl;
    return 0;
}
Useful if you want to assign a char array to an existing string.

Complexity: O(n) time, O(n) space

Time Complexity

The string constructor reads each character until it finds the null terminator, so it takes time proportional to the length of the char array.

Space Complexity

The string allocates memory to store all characters, so space used is proportional to the char array length.

Which Approach is Fastest?

Using the constructor or assignment operator both have similar performance since they do the same work internally.

ApproachTimeSpaceBest For
std::string constructorO(n)O(n)Creating new string from char array
Assignment operatorO(n)O(n)Simple syntax for new string
std::string::assign()O(n)O(n)Assigning to existing string
💡
Make sure your char array ends with a null character \0 so the string knows where to stop.
⚠️
Forgetting the null terminator \0 in the char array causes undefined behavior when converting to string.