This article shows how to convert a character array to a string in C++.
The std::string in c++ has a lot of inbuilt functions which makes implementation much easier than handling a character array. Hence, it would often be easier to work if we convert a character array to string.
Examples:
Input: char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
'r', 'g', 'e', 'e', 'k', 's' } ;
Output: string s = "geeksforgeeks" ;
Input: char s[] = { 'c', 'o', 'd', 'i', 'n', 'g' } ;
Output: string s = "coding" ;
- Method 1:
Approach:- Get the character array and its size.
- Create an empty string.
- Iterate through the character array.
- As you iterate keep on concatenating the characters we encounter in the character array to the string.
- Return the string.
Below is the implementation of the above approach.
// Demonstrates conversion// from character array to string#include <bits/stdc++.h>usingnamespacestd;// converts character array// to string and returns itstring convertToString(char* a,intsize){inti;string s ="";for(i = 0; i < size; i++) {s = s + a[i];}returns;}// Driver codeintmain(){chara[] = {'C','O','D','E'};charb[] ="geeksforgeeks";inta_size =sizeof(a) /sizeof(char);intb_size =sizeof(b) /sizeof(char);string s_a = convertToString(a, a_size);string s_b = convertToString(b, b_size);cout << s_a << endl;cout << s_b << endl;return0;}chevron_rightfilter_noneOutput:CODE geeksforgeeks
- Method 2:
The std::string has an inbuilt constructor which does the work for us. This constructor takes in a null-terminated character sequence as it’s input. However, we can use this method only at the time of string declaration and it cannot be used again for the same string because it uses a constructor which is only called when we declare a string.Approach:
- Get the character array and its size.
- Declare a string (i.e, an object of the string class) and while doing so give the character array as its parameter for its constructor.
- Use the syntax: string string_name(character_array_name);
- Return the string.
Below is the implementation of the above approach.
// Demonstrates conversion// from character array to string#include <bits/stdc++.h>usingnamespacestd;// uses the constructor in string class// to convert character array to stringstring convertToString(char* a,intsize){string s(a);// we cannot use this technique again// to store something in s// because we use constructors// which are only called// when the string is declared.// Remove commented portion// to see for yourself/*char demo[] = "gfg";s(demo); // compilation error*/returns;}// Driver codeintmain(){chara[] = {'C','O','D','E'};charb[] ="geeksforgeeks";inta_size =sizeof(a) /sizeof(char);intb_size =sizeof(b) /sizeof(char);string s_a = convertToString(a, a_size);string s_b = convertToString(b, b_size);cout << s_a << endl;cout << s_b << endl;return0;}chevron_rightfilter_noneOutput:CODE geeksforgeeks
- Method 3:
Another way to do so would be to use an overloaded ‘=’ operator which is also available in the C++ std::string.Approach:
- Get the character array and its size.
- Declare a string.
- Use the overloaded ‘=’ operator to assign the characters in the character array to the string.
- Return the string.
Below is the implementation of the above approach.
// Demonstrates conversion// from character array to string#include <bits/stdc++.h>usingnamespacestd;// uses overloaded '=' operator from string class// to convert character array to stringstring convertToString(char* a,intsize){string s = a;returns;}// Driver codeintmain(){chara[] = {'C','O','D','E'};charb[] ="geeksforgeeks";inta_size =sizeof(a) /sizeof(char);intb_size =sizeof(b) /sizeof(char);string s_a = convertToString(a, a_size);string s_b = convertToString(b, b_size);cout << s_a << endl;cout << s_b << endl;return0;}chevron_rightfilter_noneOutput:CODE geeksforgeeks
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Convert the string into palindrome string by changing only one character
- Convert Character Array To String In Java
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Replace every character of a string by a different character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace all occurrences of character X with character Y in given string
- Count of substrings having the most frequent character in the string as first character
- Nearest smaller character to a character K from a Sorted Array
- How to convert a single character to string in C++?
- Number of ways to convert a character X to a string Y
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Minimum operations required to convert all characters of a String to a given Character
- Convert vowels into upper case character in a given string
- How to convert given number to a character array
- Longest Common Prefix using Character by Character Matching
- Check input character is alphabet, digit or special character
- Count substrings that starts with character X and ends with character Y
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

