Javatpoint Logo
Javatpoint Logo

Char array to string in C++

"Char" data type or a character data type is used to store letters, unlike numbers and integers, which are stored in integer and floating-point or true-false value in Booleans.

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

We can initialize char variables using -

char ch2{ 'a' }; To print character "a".

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

char ch{'5'}; To print number character "5".

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
  • 'while' loop,
  • '=' operator from the string class
  • using custom function.

Syntax for using 'c_str()' and 'strcpy()' function is given below:

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' a 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.

Code:-

Output:

Char array to string in C++

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.

Code:-

Output:

Char array to string in C++

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

Code:-

Output:

Char array to string in C++

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

Code:-

Output:

Char array to string in C++

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.

Code -

Output:

Char array to string in C++

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.

CODE -

Output:

Char array to string in C++




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA