Convert string to Char Array in C++

String to char array C++

In this article, we are going to see how to convert string to char array in C++.

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[0]=’J’
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,

In C,
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:

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++.

Output:

Enter first string
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.

Output:

Enter first string
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.

Output:

Input string:
java2blog
Converted to char array
java2blog

Using copy() function of library

Another way is to use copy() function of CPP library

Output:

Input string:
java2blog.com
Converted to char array
java2blog.com

Using copy() function of library

We can also use copy() function of library like the following way:

Input string:
java2blog.com
Converted to char array
java2blog.com

That’s all about how to convert string to char array in C++.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *