In this article, we will discuss different ways to convert an int array to a string in C++.
Table of Contents
Problem Description
We have given an array that contains n numbers of integer elements. Now, we have to convert this integer array to a string.
Input
int arr[] = { 5, 4, 3, 2, 1 };
Output
Frequently Asked:
"54321"
There are different techniques to convert an int array to a string. Let’s discuss them one by one.
Convert int array to string using to_string()
In this approach, the array is iterated using for-loop and each element of the array is concatenated to form a resultant string. The to_string() function converts the integer values to string, so that concatenation is possible.
Time Complexity: O(n)
Space Complexity: O(1)
#include <iostream> using namespace std; string convertArrayToString(int arr[], int n) { // declaring an empty string string s = ""; // iteration using for-loop for (int i = 0; i < n; i++) { // concatenation of string s += to_string(arr[i]); } // return the string return s; } int main() { int arr[] = { 5, 4, 3, 2, 1 }; 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++:
54321
In this example, we converted an int array to a string.
Convert int 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 stringstream and convertArrayToString() function returns the stringstream as a string.
Time Complexity: O(n)
Space Complexity: O(1)
#include <iostream> #include <sstream> using namespace std; string convertArrayToString(int 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() { int arr[] = { 5, 4, 3, 2, 1 }; 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
54321
In this example, we converted an int array to a string.
Convert int array to string using push_back()
In this approach, the push_back() function of string class is used for concatenation of string. The elements of the array are concatenated one by one with the help of for-loop. The ascii value of ‘0’ character is added to convert integer to character.
Time Complexity: O(n)
Space Complexity: O(1)
#include <iostream> using namespace std; string convertArrayToString(int 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] + '0'); } // return the string return str; } int main() { int arr[] = { 5, 4, 3, 2, 1 }; 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
54321
In this example, we converted an int array to a string.
Summary
In this article, we have seen various methods for converting an integer array to a string with the use of some string functions and stringstream.