Convert String to Char Array in C++



A string in C++ is a group of characters written inside double quotes like "Hello" and characters are the single letters or symbols in a string. Converting a string to a char array means putting each character into a simple list (array) of characters. In this article, we will show you how to convert a string to a character array in C++.

For example, if we have the string "Hello@TP", converting it to a char array means storing each character 'H', 'e', 'l', 'l', 'o', '@', 'T', and 'P' separately in an array.

How to Convert a String into a Character Array?

In C++, we can convert a string into a character array in different ways. Below are the approaches we cover:

Using c_str() Function

In this approach, we use the c_str() function of the string class. This function returns a pointer to the string's characters stored as a constant array. We use this pointer to convert the string into a character array.

Example

Below is a complete C++ program where we create a string, get its pointer using c_str(), and print it as a character array using cout directly.

#include <iostream>
using namespace std;

int main() { 
    string str = "code";
    
    // Convert the string to a C-style character array
    const char* charArray = str.c_str();
    
    cout << "Characters in the array: "<< endl;
    
    // Print each character in single quotes
    for (int i = 0; charArray[i] != '\0'; i++) {
        cout << charArray[i] << endl;
    }
    cout << endl;
    return 0;
}

Below is the output of the above program using c_str() to convert the string to a character array.

Characters in the array: 
c
o
d
e

Time Complexity: O(1).

Space Complexity: O(1).

Using strcpy() Function

In this approach, we use the C-style strcpy() function from the <cstring> library to copy the contents of a string into a character array. This allows us to modify the character array if needed.

Example

Here is a complete C++ program where we use strcpy() to convert a string into a character array.

#include <iostream>
#include <cstring>
using namespace std;

int main() { 
    const char* original = "Code";    
    char charArray[100];            // Array to hold copied characters

    // Copy the string using strcpy only
    strcpy(charArray, original);

    cout << "Characters in the array: "<< endl;
    for (int i = 0; charArray[i] != '\0'; i++) {
        cout << charArray[i] << endl;
    }
    cout << endl;
    return 0;
}

Below is the output of the program where we use strcpy() to copy the string into a character array.

Characters in the array: 
C
o
d
e

Time Complexity: O(n), where n is the length of the string

Space Complexity: O(n), for the new character array

Using Range-Based Loop

In this approach, we use a loop to go through the string and copy each character into a character array one by one.

Example

Below is a complete C++ program where we convert a string to a character array using a loop.

#include <iostream>
using namespace std;

int main() {
    string str = "Sample";
    char charArray[100];
    // Copy each character from the string to the char array one by one
    for (int i = 0; i < str.length(); i++) {
        charArray[i] = str[i];
    }
    cout << "Characters in the array: "<< endl;
    // Print each character individually
    for (int i = 0; charArray[i] != '\0'; i++) {
        cout << charArray[i] << endl;
    }
    cout << endl;
    return 0;
}

The output below shows the string converted to chracter array using loop.

Characters in the array: 
S
a
m
p
l
e

Time Complexity: O(n) because we visit each chracter array one by one.

Space Complexity: O(n) because we store the chracters in array.

Using vector

In this approach, we use a std::vector<char> to store each character of the string. This is helpful when we don't know the string's size beforehand and want a dynamic character array.

Example

In this example, we use the string's begin() and end() iterators to create a vector of characters, then print each character from the vector.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    string str = "Vector";
    // Convert the string to a vector of characters
    vector<char> charVec(str.begin(), str.end()); 
    cout << "Characters in the vector: "<< endl;
    for (char c : charVec) {
        cout << c << endl;
    }
    cout << endl;
    return 0;
}

Below is the output of the program where the string is converted into a vector of characters.

Characters in the vector: 
V
e
c
t
o
r

Time Complexity: O(n) because it copies and prints each character one by one.

Space Complexity: O(n) because a new vector of size n is created to store the characters.

Updated on: 2025-05-28T18:05:05+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements