C++

How to Convert Char* to String in C++

In programming, the user might occasionally need to convert from char to string in C++. The extended operators of a string object can be used in a variety of ways which simplifies the handling of strings. A char* can be converted to a string in a number of different ways. In this article, some of the methods are explained and implemented for char* to string conversion in C++.

Method 1: Assigning the Character Array to the String Variable

The most simple way to convert char* to a string in C++ is to simply use the array of characters and assign it to a string variable.

Let’s create a program to see how it works.

#include <iostream>

using namespace std;

int main() {

  string s = {'P','r','o','g','r','a','m', 'm', 'i','n','g'};

  cout << s;

}

The “iostream” library package is included for basic input/output operations. Also, the “std” namespace is used. After that, we define the main() function with the “int” type which means that the program ultimately returns some integer value. Inside the brackets of the main() function, we initialize a string variable “s” with a list of characters. The characters in the character array are separated by commas. So, the character list that we provide is “p”, “r”, “o”, “g”, “r”, “a”, “m”, “m”, “i”, “n”, and “g”. To print the values that are stored in string “s”, we use the “cout” statement.

Running the program gives us a string that is generated by combining the character list which can be seen in the following snapshot:

Method 2: Converting Char** to String by Utilizing the String.Append() Method

The second method that we discuss here to convert char* to a string is using the string.append() function. We first create an empty string. An array of characters is then initialized. Lastly, using the string.append() method, the character array is appended to the string.

The program to implement this method is given here:

#include <iostream>

using namespace std;

int main() {

  string x;

  char arr[] = {'I','n','t','e','r','e','s','t','i','n','g','\0'};

  x.append(arr);

  cout << x;

}

The “iostream” package is included in the program, and the “std” namespace is also utilized. The main() function of the program starts. We carry out the necessary tasks between the brackets of the main() function. So, we first create an empty string with the name “x”. The next line of the code contains a char* pointer array “arr” that holds a list of character values which are “I”, “n”, “t”, “e”, “r”, “e”, “s”, “t”, “i”, “n”, “g”, and “\0”. Here, the last element is a null value and is used for the termination of the string. Now, to append this character array to the string, we invoke the string.appaend() method. The string name is given with the “append” function as “x.append” and the “arr” array to be appended is supplied within the function brackets. To display the string on the terminal, the “x” string is used with the “cout” statement.

The output string is provided in the following image:

Method 3: Converting Char* to String by Utilizing the Assignment (=) Operator

The third method that we are going to elaborate on is using the assignment operator to convert char* to a string. A character array is declared and initialized with a list of character values. Then, each element of the char* array is assigned to the string variable by employing the assignment operator.

Let’s work on its implementation.

#include <iostream>

using namespace std;

int main()

{

  Char* conv = "C++ is a programming language.";

    string st;

    st = conv;

    cout << st;

    return 0;

}

After including the required libraries, the main() function begins. Inside the main() function, we declare a char* pointer array “conv” which stores the character values as “C++ is a programming language.” Then, we declare the “st” string. This “st” string is assigned with the values of the char* array “conv” using the assignment operator (=). Each character value of the char* array is stored in the corresponding index location in the “st” string. The string is displayed using the “cout” statement.

Here is the output string:

Method 4: Converting Char* to String Using the String.Assign() Function

Using the string.assign() method, we can also convert char* to a string. We first declare an array of characters and store some characters in it. Then, a string will be declared. The string.assign() is then called. This function accepts two parameters: the first and the last index value of the char* array.

The implemented program is as follows:

#include <iostream>

using namespace std;

int main()

{

  Char* y = "This is the era of AI.";

  string a;

  a.assign(y, y + 22);

  cout << a;

  return 0;

}

The header files are included in the program. In the main() function, we initialize a character pointer array “y” which stores the “This is the era of AI”. Then, a string variable “a” is declared. Proceeding, we invoke the string.assign() method. The string name is here. The char* array, whose values are assigned to the string, is supplied within the round braces.

Within the function brackets of the a.assign() method, two parameters are passed. It takes in the first index and the last index position of the char* array. So, we provide it with the first value as “y” and the last index position as “y+22” where 22 is the size of the array, both values are separated by a comma. The values from the char* array are assigned to the string variable. The “cout” statement prints the converted string.

The resultant string is as follows:

Method 5: Converting Char* to String by Utilizing the String Constructor

In this method, the string constructor simply takes the char* as a parameter. It fills the string with consecutive copies of the char* pointer array.

After including the header files, the main() section starts. We initialize a char* pointer array “c” with “This is a string.” Then, a string constructor is called and this char* array is passed as a parameter to it. The char* pointer array’s contents are transferred to the string.

#include <iostream>

using namespace std;

int main()

{

  char* c = "This is a string.";

  string t(c);

  cout << t;

  return 0;

}

The output can be viewed in the following snapshot:

Conclusion

This post discussed the several methods to convert the char* to a string. The first approach that we covered involves just giving a string variable with a character array. In the second method, we used the string.append() method for conversion. The third method utilized the assignment operator, and the fourth method employed the string.assign() function. For the last method, the string constructor is called to convert char* to string.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.