Char Array to String in C++
Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a large number of them, including strings and characters. When dealing with C++, there will be times when you'll want to convert a character array to a string data structure to improve the efficiency and effectiveness of your code. Let's take a closer look at some of the most prevalent ways for converting char array to string externally, as well as their related examples and output, in this post. But before we get started, let's review what string and char are in C++.
What do you mean by Char ?
In C++, a character is a primitive data type that is also referred to as the 'char' data structure. A single character can be kept in a char data structure, which can represent the alphabet, special characters, numerals, escape sequences, and so on. It is a data structure that takes up only one byte of memory, although the character can also be stored as an integer implicitly. It's worth noting that the ASCII encoding standard is employed by the char data structure in C++. Let us explain what ASCII stands for before you start scratching your head over what it is. ASCII stands for American Standard Code for Information Interchange. It specifies a specific method of converting English characters to integers. For example, the ASCII value of letter ‘d’ is ‘100’, but the ASCII value of 'D' is '68'.
Example :
#include<iostream>
using namespace std;
int main()
{
// declaring a character
char c;
//giving character value as an input
cin>> c;
//displaying the value of character entered
cout<<c;
}
Output :
T
T
Explanation :
In the above example, we declared a char named c. Then asked the user to provide a value for that char c. The value given by the user will then be printed as the output.
What do you mean by Char Array ?
The sequence or collection of characters in an array in the C++ computer language. A terminated character appears at the conclusion of a character array, indicating that it is the array's last character. Like an array of integers or floats, each index in the array includes a single letter or array.
What do you mean by Strings ?
In the Classic C++ Library, there is a class called string. Please remember that in C++, string is a class, not a fundamental data type. Strings are handled and manipulated using the string class. A defined as a collection of characters that ends with a null character in technical language.
Example :
#include<iostream>
#include <string>
using namespace std;
int main()
{
// declaring a string st
string st;
//Taking input as value for string st
cin>> st;
//displaying the value given to string str
cout<<st;
}
Output :
Computer
Computer
Explanation :
In the above example, we declared a string named st. Then we asked the user to provide a value for that string st. The value given by the user will then be printed as the output.
Converting Char Array to String in C++ :
We'll give some examples of how to convert a character array to a string.
Method 1: To the Char Array, assign String Literal.
We can immediately assign a char array with a list of characters to the string variable to convert it to a string.
Example :
#include <iostream>
using namespace std;
int main() {
string st = {'c', 'o', 'm', 'p', 'u', 't', 'e', 'r'};
cout << st;
}
Output :
computer
Explanation :
In the above example, we provide an array of chars directly in the program as a string st and this string was printed as a whole.
Method 2: Using the function string.append().
The method of creating an empty new string and attaching the char array to it to convert the char array to a string. The char array now contains characters in the string.
Example :
#include <iostream>
using namespace std;
int main() {
string st;
char charAr[] = {‘c', ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’};
st.append(charAr);
cout << str;
}
Output :
computer
Explanation :
In the above example, we created an empty new string called st and attached a char array called charAr[] to that string to it to convert the char array to a string. Resulting in printing the char array that contains characters in the string.
Method 3: Append each Char Array element to a String.
To assign each char in a string to a char array, use a looping statement.
Example :
#include <iostream>
using namespace std;
int main() {
string st = "";
char charAr[] = {'c', 'o', 'm', 'p', 'u', 't', 'e', 'r'};
for(char chr: charAr) {
st += chr;
}
cout << st;
}
Output :
computer
Explanation :
In the above example, we used the C++ for loop. To add each char of the char array charAr[] to the string st for each statement. With the same repetitive process we printed the whole string as one.