Skip to content
Related Articles

Related Articles

Convert character array to string in C++
  • Difficulty Level : Basic
  • Last Updated : 28 May, 2019

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:
    1. Get the character array and its size.
    2. Create an empty string.
    3. Iterate through the character array.
    4. As you iterate keep on concatenating the characters we encounter in the character array to the string.
    5. Return the string.

    Below is the implementation of the above approach.






    // Demonstrates conversion
    // from character array to string
      
    #include <bits/stdc++.h>
    using namespace std;
      
    // converts character array
    // to string and returns it
    string convertToString(char* a, int size)
    {
        int i;
        string s = "";
        for (i = 0; i < size; i++) {
            s = s + a[i];
        }
        return s;
    }
      
    // Driver code
    int main()
    {
        char a[] = { 'C', 'O', 'D', 'E' };
        char b[] = "geeksforgeeks";
      
        int a_size = sizeof(a) / sizeof(char);
        int b_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;
      
        return 0;
    }
    Output:
    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:

    1. Get the character array and its size.
    2. 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.
    3. Use the syntax: string string_name(character_array_name);
    4. Return the string.

    Below is the implementation of the above approach.




    // Demonstrates conversion
    // from character array to string
      
    #include <bits/stdc++.h>
    using namespace std;
      
    // uses the constructor in string class
    // to convert character array to string
    string convertToString(char* a, int size)
    {
        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 
        */
      
        return s;
    }
      
    // Driver code
    int main()
    {
        char a[] = { 'C', 'O', 'D', 'E' };
        char b[] = "geeksforgeeks";
      
        int a_size = sizeof(a) / sizeof(char);
        int b_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;
      
        return 0;
    }
    Output:
    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:

    1. Get the character array and its size.
    2. Declare a string.
    3. Use the overloaded ‘=’ operator to assign the characters in the character array to the string.
    4. Return the string.

    Below is the implementation of the above approach.




    // Demonstrates conversion
    // from character array to string
      
    #include <bits/stdc++.h>
    using namespace std;
      
    // uses overloaded '=' operator from string class
    // to convert character array to string
    string convertToString(char* a, int size)
    {
        string s = a;
        return s;
    }
      
    // Driver code
    int main()
    {
        char a[] = { 'C', 'O', 'D', 'E' };
        char b[] = "geeksforgeeks";
      
        int a_size = sizeof(a) / sizeof(char);
        int b_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;
      
        return 0;
    }
    Output:
    CODE
    geeksforgeeks
    
    Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
    My Personal Notes arrow_drop_up
Recommended Articles
Page :