C++

Char array to string C++

Both Character arrays and strings, in C++, are used to store characters and letters. But there is a difference in them; this can be related by describing them as follows:

Character array

In the C++ programming language, the series or bundle of characters in the array. A character array contains a terminated character at the end of the array, which shows that this is the last character in the array. Each index in the array contains a single letter or array, just like an array of integers or floats.

Strings

Strings contain the characters that are encoded through quotes. In C++, a programming language, two types of strings are present. One is the object of the String class, a standard C++ string class library. The other one is C-string( C-style Strings). Strings are the most useful data type among others used by having a string library.

Implementation

We will include some examples that will elaborate on converting the character array into the string using the Linux operating system.

Example 1

To convert a character array into a string, we need a library to use in the source code; this header file will help us use the input-output streaming plus allow the string functionality to be combined in a single library.

#include <bits/stdc++.h>

Then a function will be created here that will convert the character array into a string. The function’s return type is a string; it shows that the converted string will be the returned item. The function will take the size of the array and a character array. Inside the function, an empty string is created. As we need to convert the character array to a string, we need to have a string that contains no element.

String s = " "

Where is the object of the string? We can perform any operation to/from the string through this object. All the characters from the character array will be transferred to the string using a for a loop. As the character array contains each character in the respective index, in each iteration, a single character from an index of the character array is transferred to the string array. For each next character, the concatenation process takes place.

Concatenation is a built-in feature of a string in which all the string letters are added together. Or in other words, when two strings join together, this is concatenation. So the empty string will be filled through for loop, the loop will iterate till the last terminating character or the size of the character array.

S = s + a[i];

This statement shows that each existing character in the string will be added to the new one present in the specified index of the character array. In the main program, we have initialized two character arrays. These both are of different styles, and no specific size is mentioned; it means the size of the array is not fixed. One contains one letter separately for each index. Whereas the second character array includes a single item of many letters. As the size is not known, we need to calculate the current size of an array.

Int a_size = sizeof (a) / sizeof (char);

This is how the size of the character array is calculated. Now we will make a function call; the character array and the calculated array size are transferred as arguments. A string will get the returned value.

String s_a = converttostring(a, a_size);

Now save the file, and then the compilation process will take place through a g++ compiler needed to execute a file of C++ codes in the Ubuntu Linux system. ‘char. c’ is the name of the file.

$ g++ -o char char.c

$ ./char

On execution, you can see that both the character arrays are converted into a string.

Example 2

As we know that the string class has a string in-built constructor, this method is only used when we declare a string; later on, this is of no use. Just like the simple constructors we use in the class, they are executed once when the object of the class is created. We have used a simple example here. We have declared a function using the same header file in the program; this will take an array as a parameter. Inside the function, we will declare a string through the object of the string, whereas the parameter contains the character array for the constructor.

The syntax used for this string declaration has a simple syntax:

String string_name/object (name_of_the_character_array);

String s(a);

Inside the main program, two character arrays are declared. Both are the same as we have introduced them in the previous example. Both the function calls will contain only the character array as a parameter, as there is no need for the size of the array, so it is not calculated.

String s_b = converttostring(b);

The function call is the same for both character arrays.

Now save and then execute the file. The resultant value for the character array will contain both the arrays together, making a single string, whereas the second array is displayed.

Example 3

This example contains the use of an assignment operator, also known as an overloaded operator. This feature is available in the library stdc++ that we include in our source code so that all the features of string become easy to implement. All these characters are to be copied in the function that will take the array as a parameter. Declare an empty string and then use the assignment operator to assign all the characters to the array.

String s = a;

After that, return the string to the main program; on the main side, both arrays are initialized, and then the function call is made.

Cout << s_a;

This will display the contents of the string.

Example 4

This is a simple method to use a default function of strings. That is an append() function. The function works in a way that it adds the characters at the end of the existing ones, so by doing this, a string is developed. Here we will use this function to copy all the characters in the character array to the string. A string will be created here with ‘str.’ This is an empty string. A character array will be defined here with all the characters and a terminating character showing there is no further character in the array.

After that, we will use the append function. This function will take the character array.

Str.append(charArr);

In the end, the string will be displayed. On the execution of the code, you can see that all the characters present separately are combined by using this append function.

Conclusion

A character array is just like an integer or a double array, only the type of data is different, but strings are the data type that contains all the characters in sequence. While converting character array into strings, three main approaches are included in the above discussion. The easiest way of conversion is to use the ‘append’ built-in method for copying all the data from the character array to strings.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.