Char Array To String In C++ - C++ Programming Tutorial
C++ Course / Strings / Char Array To String In C++

Char Array To String In C++

BLUF: Mastering Char Array To String In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Char Array To String In C++

C++ is renowned for its efficiency. Learn how Char Array To String In C++ enables low-level control and high-performance computing in the tutorial below.

A "Char" data type, also known as a character data type, is employed to hold individual letters, as opposed to numbers and integers stored in integer and floating-point formats, or true-false values in Booleans.

Characters are of integer type, occupying 1 byte each. Printable characters range from 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.

Usage of the 'c_str' and 'strcpy' functions is outlined as follows:

Example

"string-name.c_str();".

The c_str function converts the string's contents into a C-style, non-null-terminated string, providing direct access to the internal string buffer.

We can transform a character into a string by utilizing a 'for' loop as follows:

Initially, we define a Character Array followed by specifying its size. Subsequently, we establish a pair of variables - one of type string and the other of type int. Following this, we can initiate a 'for' loop by setting the int variable to 0, ensuring that it remains less than array_size. The int variable is then incremented by one at each iteration. It is crucial to save the value in the string variable during each iteration prior to exhibiting the string variable.

Code:-

Example

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

Output:

We have the option to convert a character to a string by employing a 'while' loop through the following steps:

First initializing the Character Array followed by specifying the Array's size. After that, we create two variables - one of type string and the other of type int with an initial value of 0. A 'while' loop is employed to validate if the integer variable is less than the array's size during each iteration. The value is then stored in the string variable before exhibiting the string variable.

Code:-

Example

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

Output:

To transform a character into a string using the std::string constructor, we can achieve this by directly supplying the array to the string constructor.

Code:-

Example

#include <iostream> 
using namespace std;
int main()
{
char char_array[] = {'J','A','V','A','T','P','O','I','N','T'};
string j(char_array);
cout << j <<endl;
}

Output:

To transform a character array into a string by utilizing the '=' operator and the string class, it is necessary to assign the character array to a string variable.

Code:-

Example

#include <iostream>
  using namespace std;
   int main()
{
   char char_array[] = {'J','A','V','A','T','P','O','I','N','T'};
   string j = char_array;
   cout << j <<endl; 
}

Output:

To transform a character into a string using personalized functions, it is necessary to establish a custom function that takes two parameters. Within this function, two variables - a string and an integer - need to be defined. Subsequently, a 'for' loop should be implemented, initializing an integer variable to 0, setting the size of the integer variable to be less than the array's size, and incrementing the integer variable by one in each iteration. The custom function will ultimately yield the string. In the main function, a character array along with its size is declared, and these are then passed to the custom function. Finally, the content of the string variable, which holds the value returned by the custom function, is printed.

Code -

Example

#include <iostream>
using namespace std;
string charToString(char* arr_char, int arr_size)
{
 int i;
 string j = "";
  for(i = 0; i < arr_size; i++)
  {
  j = j + arr_char[i];
  }
   return j;
}
  int main() {
    char char_array[] = {'J','A','V','A','T','P','O','I','N','T'};
    int array_size = sizeof(char_array) / sizeof(char);
     string jtp = "";
    jtp = charToString(char_array, array_size);
    cout << jtp <<endl;
    return 0;
}

Output:

The final approach to transform a character into a string involves utilizing std::stringstream. This method involves inserting the given character into a buffer and then extracting it as a string using std::string.

CODE -

Example

#include <iostream>
#include <sstream>
int main()
{   
    char char_array[] = {'J','A','V','A','T','P','O','I','N','T'};
    std::string jtp;
    std::stringstream strStream;
    strStream << char_array;
    strStream >> jtp;
    std::cout << jtp;
    return 0;
}

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience