In this article, we are going to see how to convert string to char array in C++.
Table of Contents
String vs character array in C++
In C, we used string as character array. String used to be a collection of characters terminated by a NULL character.
char str[]=”Java2Blog”
Here,
str[1]=’a’
str[2]=’v’
str[3]=’a’
str[4]=’2’
str[5]=’B’
str[6]=’l’
str[7]=’o’
str[8]=’g’
str[9]=’\0’
If it was merely a character array then, the NULL character would have been missing.
Another way we can say,
For a character array,
char str[] = {‘J’, ‘a’, ‘v’, ‘a’, ‘2’, ‘B’, ‘l’, ‘o’, ‘g’};
For a string,
char str[] = {‘J’, ‘a’, ‘v’, ‘a’, ‘2’, ‘B’, ‘l’, ‘o’, ‘g’, ‘\0’};
It is the case for C. But in C++, we have additional advantage to use ‘string’ as a datatype. So there’s differences between string and character array in C++. We can list few of those out here.
Character array is collection of data type ‘char’. ‘string’ is itself a data type.
We can use data type operations (‘+’, ‘==’ etc.) on ‘string’, but not on character array.
To find the length, we can use .length() for ‘ string’. But for character array, there will be predefined size or have to compute the length using for loop like following:
1 2 3 4 5 6 7 |
int length=0; for(int i=0;str[i]!=’\0’;i++){ length++; } cout<<”string(character array length: ”<<length></length> |
Convert string to char array in C++
String is used as a data type in C++. Here goes some quick review about string in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <bits> using namespace std; int main(){ //declare string string str1,str2,finalstring; cout<<"Enter first string\n"; //input strings as data type cin>>str1; cout<<"Enter second string\n"; cin>>str2; //operating on string like other datatype finalstring=str1+str2; //output string cout<<"Concatenated string: "<<finalstring></finalstring></bits> |
Output:
java2blog
Enter second string
.com
Concatenated string: java2blog.com
Using for loop
To convert a C++ string into char array the general procedure is:
- Input the string
- Define a char array with some predefined size or with size = string length
- Iterate through the string and build the char array
- Output the char array
Following is the code & output for the above procedure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <bits> using namespace std; int main(){ //declare string string str; cout<<"Enter first string\n"; //input strings as data type cin>>str; //size=string length int size=str.length()+1; //plus 1 to adjust the null character of c string //define character array char arr[size]; //iterate through the string and build the character array for(int i=0;i< str.length();i++){ arr[i]=str[i]; } arr[size-1]='\0'; cout<<"converted to character array\n"; //print the character array for(int i=0;i<size cout element of character array: array string: return></size></bits> |
Output:
java2blog
converted to character array
1th element of character array: j
2th element of character array: a
3th element of character array: v
4th element of character array: a
5th element of character array: 2
6th element of character array: b
7th element of character array: l
8th element of character array: o
9th element of character array: g
10th element of character array:
Converted character array string: java2blog
Notice that 10th character in the char array is blank in output, because it’s a null character.
Using c_str() and strcpy()
c_str() is a library function which converts C++ string data type to a C strng. Then we can copy it to character array using strcpy() which a cstring library function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <bits> //it includes all the libraries using namespace std; int main(){ //declare string string str; //input str cout<<"Input string:\n"; cin>>str; int size=str.length()+1; //plus 1 to adjust the null character of c string char arr[size]; //strcpy to copy the converted c string in character array strcpy(arr,str.c_str()); cout<<"Converted to char array\n"; cout<<arr></arr></bits> |
Output:
java2blog
Converted to char array
java2blog
Using copy() function of
Another way is to use copy() function of CPP library
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <bits> //it includes all libraries using namespace std; int main(){ //declare string string str; //input str cout<<"Input string:\n"; cin>>str; int size=str.length()+1; //plus 1 to adjust the null character of c string //initialize array char arr[size]; //copy() to copy the entire c++ string in the character array copy(str.begin(),str.end(),arr); //add null character arr[size-1]='\0'; cout<<"Converted to char array\n"; cout<<arr></arr></bits> |
Output:
java2blog.com
Converted to char array
java2blog.com
Using copy() function of library
We can also use copy() function of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <bits> //it includes all the libraries using namespace std; int main(){ //declare string string str; //input str cout<<"Input string:\n"; cin>>str; int size=str.length()+1; //plus 1 to adjust the null character of c string //initialize array char arr[size]; //string.copy() to copy the entire c++ string in the character array str.copy(arr,str.length()); //add null character arr[size-1]='\0'; cout<<"Converted to char array\n"; cout<<arr></arr></bits> |
java2blog.com
Converted to char array
java2blog.com
That’s all about how to convert string to char array in C++.