Home › Tutorials & Guides › Coding Tutorials › Char array to string in C++

Char array to string in C++

This article shows that C++ Program to convert Char array to string. ā€œcharā€ data type or a character data type is used to store letters, unlike numbers and integers, which are stored in ā€œintā€, ā€œdoubleā€ and ā€œfloatā€ or true-false value in ā€œboolā€.

Characters are integer type in nature their size is one-byte and printable characters are ā€œ, #, $, %, &, ā€˜, ( ) ,*, +,(,) , -, ., / , 0, 1, 2, 3, 4 ,5 ,6 ,7 ,8, 9 ,:, ;, <, = ,> ,?, @,A, B, C, D, E, F, G, H, I, J, K, L, M, N, O,  P, Q, R, S, T, U, V, W, X, Y, Z, [ ,\, ], ^, _,  `, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,  p, q, r, s, t, u, v, w, x, y, z, {, |, }, ~, and space.

We can initialize char variables using –

char ch2{ ā€˜x’ }; To print character ā€œxā€.

char ch1{ 100 }; To print the value at ASCII code 100.

char ch{’10’}; To print number character ā€œ10ā€.

C++ provides us with the following techniques to convert a char array to a string:

  • Using ā€˜c_str()’ and ā€˜strcpy()’ function
  • Using a ā€˜for’ loop
  • Using a ā€˜while’ loop,
  • Using ā€˜=’ assign operator and string class
  • Using custom function.

Syntax for using ā€˜c_str()’ and ā€˜strcpy()’ function is given below:

    "string-name.c_str();".  

where, c_str() converts the contents of a string as C-style, non-terminated string. It gives direct access to the internal string buffer. 

We can convert character to a string using ā€˜for loop’ by – 

First, declaring the Character Array and then assigning the size of the Array. Then, we declare two variables, one string type, and another int type. After this, we can use the ā€˜for’ loop by assigning 0 to the int variable where the int variable will have less value than array_size and we increase int variable value by one at every iteration. We have to store the value in the string variable on every iteration before displaying the string variable.

Calculator Program in C++.

Example:

    #include <iostream>  
    using namespace std;  
    int main()   
    {  
        char char_array[] = {'T','E','C','H','Y','I','N','C'};  
        int array_size = sizeof(char_array) /  
        sizeof(char);  
        string s = "";  
        int i;  
        for(i = 0; i < array_size; i++)  
       {  
        /** retrieving and merging the value of character array on position 'i'*/    
        s = s + char_array[i];  
        }  
        cout << s <<endl;  
    }   

Output:

TECHYINC

We can convert char to a string using ā€˜while loop’ by –

First declaring the Character Array and then assigning the size of the Array. Then, we declare two variables, one string type, and another int type with value 0. We use ā€˜while’ loop to check int variable less than array_size on every iteration and store the value in a string variable before displaying the string variable.

Example:

    #include <iostream>  
    using namespace std;  
    int main()   
    {  
        char char_array[] = {'T','E','C','H','Y','I','N','C'};  
        int array_size = sizeof(char_array) / sizeof(char);  
        string s = "";  
        int i = 0;  
        while(i < array_size)  
      {  
            /**retrieving and merging the value of character array on position `i`*/  
            s = s + char_array[i];  
            i++;  
        }  
        cout << s <<endl;  
    }  

Output:

TECHYINC

To convert character to a string using std::string constructor, we simply pass the array to string constructor.

Example:

    #include <iostream>   
    using namespace std;  
    int main()  
    {  
    char char_array[] = {'T','E','C','H','Y','I','N','C'};  
    string s(char_array);  
    cout << s <<endl;  
    }  

    Output:

TECHYINC

To convert a character array into a string using the ā€˜=’ operator and string class, we have to pass character array to string variable.

Example:

    #include <iostream>  
      using namespace std;  
       int main()  
    {  
       char char_array[] = {'T','E','C','H','Y','I','N','C'};  
       string s = char_array;  
       cout << s <<endl;   
    }    

Output:

TECHYINC

To convert character to a string using custom functions, we have to create a custom function with two parameters. Inside the custom function, we have to declare two variables string and integer. Then we use the ā€˜for’ loop, where we assign 0 to an int variable, int variable’s size to be less than array_size, and int variable’s value increasing by one at each iteration. The function will return the string. For the primary function, we declare the character array and its size, then we pass character array and its size to the custom function. At last, we print the string variable which stores the returned value of the custom function.

Example:

    #include <iostream>  
    using namespace std;  
    string charToString(char* arr_char, int arr_size)  
    {  
     int i;  
     string s = "";  
      for(i = 0; i < arr_size; i++)  
      {  
      s = s + arr_char[i];  
      }  
       return s;  
    }  
      int main() {  
        char char_array[] = {'T','E','C','H','Y','I','N','C'};  
        int array_size = sizeof(char_array) / sizeof(char);  
         string str = "";  
        str = charToString(char_array, array_size);  
        cout << str <<endl;  
        return 0;  
    }  

Output:

TECHYINC

The last method to convert char to string is achieved by using std::stringstream. We use this function to insert the input character into a buffer and then take the character from the buffer as a string using std::string.

Example:

    #include <iostream>  
    #include <sstream>  
    int main()  
    {     
        char char_array[] = {'T','E','C','H','Y','I','N','C'};  
        std::string str;  
        std::stringstream strStream;  
        strStream << char_array;  
        strStream >> str;  
        std::cout << str;  
        return 0;  
    }  

Output:

TECHYINC
Note: If you interested lo learn C++ through web, You can click here

Conclusion:

Hi guys, I can hope that you can know that how to write a C++ Program to convert Char array to string. If you like this post as well as know something new so share this article in your social media accounts. If you have any doubt related to this post then you ask in comment section.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *