| by Arround The Web | No comments

Char Array to String in C++

In the C++ programming language, many different types of data types are used to store the data. These data types are built-in datatypes. In the C++ language, one of these basic data types is “char” which is known as the character that has a buffer size of 1 byte. Whereas, “string” is not a built-in data type; it functions in a way that is similar to one when used simply. In simple words, it can store the characters similar to character arrays and allows the users to perform the operations on strings that are not possible with character arrays. Because the user sometimes wants to supply the string as an argument, we first transform the character array into a string. It is simpler to manipulate the strings when they are converted to a string object since we can then use a variety of methods and have overloaded operators such as the += operator method, pushback method, append method, etc.

Various Techniques to Transform a Character Array to a String

Technique 1: Using the “=” Operator

To convert the array of the character type to a string, we utilize the “=” assignment operator.

Syntax:

Here is the integration rule of the “=” assignments operator while converting the character array to a string. We pass the character array variable which holds the input character elements to the string type variable.


Example:

Let’s do a simple example of character array to string conversion in C++ programming language. When starting to implement the program’s code, we first include the basic header file which is “#include <iostream>”. Then, we add the namespace directive so it prevents in giving the same name to the other functions, variables, etc. Next, we call the main function and write the driver code in the main() function body. Initially, we create a character type array called “ch” and enter the elements in it. Then, we declare another variable of “str” string type and assign the “ch” array to the “str” string variable. Now, the elements of “ch” are in “str”. We invoke the cout() function and supply the “str” variable because we want to display the elements of the “str” parameter.

#include <iostream>  
using namespace std;  
int main()  
{  
   cout << "*---Coversion of Char Array to String Using '=' Operator---*" << endl;
   char ch[] = {'L', 'i', 'n', 'u', 'x'};  
   string str = ch;  
   cout << "\nThe converted character string is: " << str <<endl;
   return 0;
}

 
The following is the output of the previously-compiled program which is “Linux” of string type:


Technique 2: Using the “+” Operator

This technique of converting the character array to a string by utilizing the “+” operator is appended in the following:

Syntax:

The following is the syntax of storing the character array into a string using the “+” operator. We first declare a string-type variable and then assign the empty string to it using the double quotation marks. Next, we add the string variable and character array. Then, we store it in the string variable.


Let’s implement the following example and see how this works in the conversion of character to string.

Example:

In this example, we implement another technique which is the use of the “+” operator in C++ programming language. The implementation of this example is slightly different from the previously-implemented program, but the output is the same. First, we have the basic library and naming convention in the program and we call the main() function. In line 9, we declare an array of character types and store some elements in it. We initialize another variable of string type and assign an empty string to it. Then, we use the for loop to iterate through the character array and concatenate the character array with the empty string. To print the value of the converted string, we use the cout() method and return 0 at the end of the program.

#include <iostream>
using namespace std;
int main()
{  
    cout << "*---Conversion of Character Array to String using '+' Operation---*" << endl; 
    char ch[] = {'L', 'i', 'n', 'u', 'x'};
    string str = " ";
    for (int i = 0; i < sizeof(ch); i++)
    {
        str = str + ch[i];
    }
    cout<<"\nThe Converted string is: " << str;
    return 0;
}

 
Here is the output of the executed program:


Technique 3: Using the Constructor

In this technique, we use the built-in constructor of the C++ language so that we can convert the array of a character type to a string.

Syntax:

To implement the constructor to get the converted string, we first have to learn the writing rule of the constructor in C++ language because it is a case-sensitive language. We first write the string datatype to declare the string variable. In the constructor brackets, we pass the character array that we want to convert to the string.


Example:

This is the same example that we already previously implemented. The only difference is that we now use a constructor to convert the character array.

#include <iostream>
using namespace std;
int main()
{  
    cout << "*---Conversion of Character Array to String Use of Constructor---*" << endl;
   
    char ch[] = {'L', 'i', 'n', 'u', 'x'};
    string str(ch);
    cout<<"\nThe Converted string is: " << str;
    return 0;
}

 
A string of characters that ends with the null character is the input parameter of the constructor.


Technique 4: Using the Append() Function

To convert the character array to the string, we use another technique which is the append() function of C++ language. It is the built-in function that is used to add the character at the end of the string.

Syntax:

To implement the append() function for the conversion of the character string, we call the append() function and pass the character array variable in it. After that, we concatenate the append() function with the string variable.


Example:

#include <iostream>

using namespace std;

int main()
{  
    cout << "*---Conversion of Character Array to String by using append() Function---*" << endl;
   
    char ch[] = {'L', 'i', 'n', 'u', 'x'};
    string str;
   
    str.append(ch);
    cout<<"\nThe Converted string is: " << str;
    return 0;
}

 
In this example, we call an append() function so that we can easily append the character array elements in the declared string end. To append the elements, we pass the character array and then display it by passing the “str” string variable in the cout() statement because it contains the elements of the character string in it.

Conclusion

In this article, we learned what character arrays and strings are in the C++ programming language. We learned how we can convert the character array in the string using a few different techniques of C++ language which are the “=” and “+” operators, constructor, and append() functions. We implemented necessary examples of every method so that we can easily understand the concept of converting the character array to a string.

Share Button

Source: linuxhint.com

Leave a Reply