In this article, we will discuss different ways to convert a char array to a string in C++.
Table Of Contents
Problem Description
We have given an array that contains n charecters. Now, we have to convert this char array to a string.
Input
char arr[] = { 'p', 'o', 'i', 'n', 't', 'e', 'r' };
Output
Frequently Asked:
pointer
There are different ways to convert char array to string. Let’s discuss them one by one.
Convert char array to string using simple addition
In this approach, the array is iterated using for-loop and each element of the array is concatenated to form a resultant string. To do this, we can just use the ‘+’ operator to add element at the end of string.
Time Complexity: O(n)
Space Complexity: O(1)
#include <iostream> using namespace std; string convertArrayToString(char arr[], int n) { // declaring an empty string string s = ""; // iteration using for-loop for (int i = 0; i < n; i++) { // concatenation of string s += arr[i]; } // return the string return s; } int main() { char arr[] = { 'p', 'o', 'i', 'n', 't', 'e', 'r' }; int n = sizeof(arr) / sizeof(arr[0]); // calling the function string s = convertArrayToString(arr, n); // print the resultant string cout << s << endl; return 0; }
Output
Best Resources to Learn C++:
pointer
In this example, we converted an array of characters to a string and printed it.
Convert char array to string using stringstream
In this approach, the concatenation of string is done using stringstream. The elements in the array are added in the string-stream and convertArrayToString()
function returns the string-stream as a string.
Time Complexity: O(n)
Space Complexity: O(1)
#include <iostream> #include <sstream> using namespace std; string convertArrayToString(char arr[], int n) { // declaring an empty stringstream stringstream ss(""); // iteration using for-loop for (int i = 0; i < n; i++) { // putting the array elements in stringstream ss << arr[i]; } // return the string return ss.str(); } int main() { char arr[] = { 'p', 'o', 'i', 'n', 't', 'e', 'r' }; int n = sizeof(arr) / sizeof(arr[0]); // calling the function string s = convertArrayToString(arr, n); // print the resultant string cout << s << endl; return 0; }
Output
pointer
In this example, we converted an array of characters to a string and printed it.
Convert char array to string using push_back() function
In this approach, the push_back()
function is used for adding element to string. The elements of the array are pushed one by one in the string with the help of for-loop.
Time Complexity: O(n)
Space Complexity: O(1)
#include <iostream> using namespace std; string convertArrayToString(char arr[], int n) { // declaring an empty string string str = "" ; // iteration using for-loop for (int i = 0 ; i < n ; i++ ) { // concatenation of string str.push_back(arr[i]); } // return the string return str; } int main() { char arr[] = { 'p', 'o', 'i', 'n', 't', 'e', 'r' }; int n = sizeof(arr) / sizeof(arr[0]); // calling the function string s = convertArrayToString(arr, n); // print the resultant string cout << s << endl; return 0; }
Output
pointer
In this example, we converted an array of characters to a string and printed it.
Summary
In this article we have seen various methods for converting a char array to a string. Happy Learning.