A C++ string variable contains a collection of characters surrounded by double-quotes. There are two types of strings used in C++.

  • C-strings, which are arrays of type char terminated by the null character \0.
  • Strings that are objects of the Standard C++ Library string class.

This tutorial will go through how to convert a char array to a string with the help of code examples.

Why Convert Char Array to String

Converting a char array to a string object allows us to use string methods and its overloaded operators, which makes string manipulation easier to do.

An example of this is getting the length of a string by calling the length() method. To get the length of a char array, we have to count its elements in a for loop or divide the size of the array by the size of an individual element.

Convert Char Array to String Using Direct Assignment of String Literal

We can use the assignment operator = to assign a char array to a string. This approach will replace the current contents of the string. Let’s look at an example below:

#include <iostream>

using namespace std;

int main() {

    string str = {'R', 'E', 'S', 'E', 'A', 'R', 'C', 'H'};

    cout <<str;
}
RESEARCH

Convert Char Array to String Using String Class Constructor

#include <iostream>
#include <iostream>

using namespace std;

int main() {

    char arr[] = {'R', 'E', 'S', 'E', 'A', 'R', 'C', 'H', '\0'};

    string str(arr);

    cout <<str;
}
RESEARCH

Convert Char Array to String Using a For Loop with += Operator

We can use the addition operator and the assignment operator to perform the function of appending. If we have a char array greater than one character, we can use a for loop to iterate over the characters in the array and append them to a string.

#include <iostream>
using namespace std;
int main()
{
    char arr[] = {'R', 'E', 'S', 'E', 'A', 'R', 'C', 'H'};

    // Size of character array

    int array_size = sizeof(arr) /
                     sizeof(char);


    // String object
    string str = "";
    int i;
    for(i = 0; i < array_size; i++)
    {
        // Retrieve and merge the value of character array at position 'i'
        str += arr[i];
    }
    cout << str <<endl;
}
RESEARCH

Convert Char Array to String using string.append()

This function performs similarly to the += operator, but it offers the functionality of adding consecutive copies of a character to the string. Adding consecutive copies does not work for char arrays, however. The syntax of the append method is as follows:

string& append (size_t n, char c)

Let’s look at using the append method with a char array and with a single character.

#include <iostream>
using namespace std;
int main() {

    string str;

    char arr[] = {'R', 'E', 'S', 'E', 'A', 'R', 'C', 'H', '\0'};

    str.append(arr);

    cout << str;
}
RESEARCH
#include <iostream>
using namespace std;
int main() {

    string str;

    char ch = 'T';

    str.append(2, ch);

    cout << str;
}
TT

Convert Char Array to String using assign()

The string::assign() function assigns characters to a string by replacing its contents like the assignment operator =. We can also use this method to assign multiple copies of a character to a string like the append() method. Let’s look at an example of using the assign method to assign a char array to a string.

#include <iostream>
using namespace std;
int main() {

    string str;

    char arr[] = {'R', 'E', 'S', 'E', 'A', 'R', 'C', 'H', '\0'};

    str.assign(arr);

    cout << str << endl;
}
RESEARCH

Convert Char Array to String using push_back()

The std::vector method push_back() inserts a new element at the end of a vector and increases the size of the vector by one. We can use the push_back() method within a for loop over the characters in the char array and push each character into a string. Let’s look at an example:

#include <iostream>
using namespace std;
int main() {

    string str;

    char arr[] = {'R', 'E', 'S', 'E', 'A', 'R', 'C', 'H'};

    int array_size = sizeof(arr) /
                     sizeof(char);

    for (int i =0; i < array_size; i++) {
        str.push_back(arr[i]);
    }
    cout << str << endl;
}
RESEARCH

Convert Char Array to String Using stringstream

We can use the stringstream class to create objects for string manipulation. A stringstream object uses a string buffer to store a sequence of characters. Let’s look at an example below:

#include <iostream>
#include <sstream>
using namespace std;
int main() {

    string str;

    char arr[] = {'R', 'E', 'S', 'E', 'A', 'R', 'C', 'H', '\0'};

    // stringstream object
    stringstream stream;

    // adding char array to buffer
    stream << arr;

    // empty the buffer into a string
    stream >> str;

    cout << str << endl;

}
RESEARCH

Summary

Congratulations on reading to the end of this tutorial. We have gone through various ways to convert a char array to a string.

For further reading on string manipulation, go to the articles:

Have fun and happy researching!