Online Tutorials & Training Materials | STechies.com
Register Login

How to Convert Char to String in C++ Language?

13 Sep 2019 9:29 am || 0

This article will help you to learn how to convert a character array to a string in C++.

Convert Char To String C++

There are many methods through which we can convert a character array to a string in C++ which are:

  • Using for loops
  • Using While loops
  • Using inbuilt std::string constructor
  • Using ‘=’ operator from the string class
  • Using custom function
  • Using std::stringstream function

1) Convert Char to a string in C++ using for loop

Program Outline

1) Declare Character Array
2) Get the Array Size
3) Declare two variables, one string type and another int type
4) Now use For loop for following

  • In for loop condition assign 0 to the int variable;
  • int variable will be less than array_size
  • Increase int variable value by one on every iteration

5) Store value in string variable on every iteration
6) Display the string variable

Example:

//Program to convert characters array to string using for loop

//including the input output library
#include <iostream>
using namespace std;

//Main function of the program
int main() {
    //Declaring and Initializing the character array
    char char_array[] = {'S','T','E','C','H','I','E','S'};
    //get the size of array
    int array_size = sizeof(char_array) / sizeof(char);
    //Declaring a String Type variable to store converted string
    string s = "";
    //Declaring a integer type variable
    int i;

    //For loop for iteration
    for(i = 0; i < array_size; i++){
        /******************
        * Getting and Merging the value of Character Array
        * on position `i`
        */
        s = s + char_array[i];
    }
    //Printing the value of String Variable
    cout << s <<endl;
} 

Output

STECHIES

2) Convert Char to a string in C++ using while loop

Program Outline

1) Declare character Array
2) Get the Array Size
3) Declare two variables, one string type and another int type with value 0
4) Use While Loop to check int variable less than array_size on every iteration
5) store value in string variable on every iteration
6) Display the string variable

//Program to convert characters array to string using While loop

//including the input output library
#include <iostream>
using namespace std;

//Main function of the program
int main() {
    //Declaring and Initializing the character array
    char char_array[] = {'S','T','E','C','H','I','E','S'};
    //get the size of array
    int array_size = sizeof(char_array) / sizeof(char);
    //Declaring a String Type variable to store converted string
    string s = "";
    //Declaring a integer type variable
    int i = 0;

    //While loop for iteration
    while(i < array_size){
        /******************
        * Getting and Merging the value of Character Array
        * on position `i`
        */
        s = s + char_array[i];
        //Increasing the value of i on every iteration
        i++;
    }
    //Printing the value of String Variable
    cout << s <<endl;
}

Output:

STECHIES

3) Convert Char to a string using std::string constructor in C++

//Program to convert characters array to a string using string constructor

//including the input-output library

#include <iostream>

using namespace std;

//Main function of the program
int main() {
    //Declaring and Initializing the character array
    char char_array[] = {'S','T','E','C','H','I','E','S'};
    //Passing the array to string cunstructor
    string s(char_array);
    //Printing the value of String Variable
    cout << s <<endl;
}

Output:

STECHIES

4) Convert Char to String using '=' operator from the string class

Another method to convert char into a string is using the ‘=’ operator from string class.

//Program to convert characters array to string

//including the input output library
#include <iostream>

using namespace std;
//Main function of the program
int main() {
    //Declaring and Initializing the character array
    char char_array[] = {'S','T','E','C','H','I','E','S'};
    //Passing character array to string variable
    string s = char_array;
    //Printing the value of String Variable
    cout << s <<endl;
}

Output:

STECHIES

5) Convert Char to String using C++ Custom Function

Program Outline

1) Create a custom function with two parameters

2) Code inside a custom function
i) declare two variables - one is string and second is an integer
ii) Use For Loop to check following

  • In for loop condition, we have assigned 0 to our int variable;
  • int variable will be less than array_size
  • Increase int variable value by one on every iteration

iii) The function will return the string

3) Code inside the primary function
i) Declare character Array
ii) Get the Array Size
iii) pass character array and array size to the custom function and custom function.
iv) print the string variable which stores the returned value of the custom function

//Program to convert characters array to a string using Custom function

//including the input output library
#include <iostream>

using namespace std;

//Custom Function with two parameters
string charToString(char* arr_char, int arr_size)
{
    //Declaring an integer type variable
    int i;
    //Declaring a string type variable
    string s = "";

    //for loop for itration
    for(i = 0; i < arr_size; i++){
        //Merging the value of character array on `i` position
        s = s + arr_char[i];
    }

    //Return the string to main function
    return s;
}


//Main function of the program
int main() {
    //Declaring and Initializing the character array
    char char_array[] = {'S','T','E','C','H','I','E','S'};
    //get the size size of array
    int array_size = sizeof(char_array) / sizeof(char);
    //Declaring a string type variable to store returned string
    string str = "";

    /* Passing the character array and size of array and storing
    * returned value into string type variable
    */
    str = charToString(char_array, array_size);

    //Printing the value of String Variable
    cout << str <<endl;

    return 0;
}


Output:

STECHIES

6) Convert Char to String using std::stringstream

Another method to convert char to string is to use std::stringstream. 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

//Program to convert characters array to a string using std::stringstream

//including the library

#include <iostream>
#include <sstream>

//Main function of the program
int main()
{
    //Declaring and Initializing the character array
    char char_array[] = {'S','T','E','C','H','I','E','S'};
    //Declaring a variablees
    std::string str;
    std::stringstream strStream;
    //Inserting characters array into the buffer
    strStream << char_array;
    //Taking the characters from buffer as string
    strStream >> str;
    //Printing the value of String Variable
    std::cout << str;

    return 0;
}

Output:

STECHIES