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

Related Posts

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusion This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays and constructors in […]

  • Write String to file in C++
    31 March

    Write String to File in C++

    Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++Conclusion The files are used to store the data permanently on a disk. This mitigates the problem of […]

  • Print Array in C++
    23 March

    Print Array in C++

    Table of ContentsIntroduction of ArraysDifferent Methods to Print an Array in C++print array in C++ by Traversing Indicesprint array in C++ Using for_each() Functionprint array in C++ Using Range-Based for Loopprint array in C++ Using Iteratorsprint array Using ostream_iterator() Function and copy() Functionprint array Using C++17 copy() Function and make_ostream_joiner() FunctionConclusion An array is a […]

  • Print Vector C++
    23 March

    Print Vector in C++

    Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusion This article discusses the vector and how to print vector in C++. Vectors are […]

  • Print Map in C++
    15 March

    Print Map in C++

    Table of ContentsMap vs Unordered MapPrint Map in C++ Using IteratorPrint Map in C++ Using Range-Based for LoopPrint Map in C++ by Overloading the ostream Insertion Operator (<<)Print Map in C++17 Using Structure Binding and Range-Based for LoopPrint Map in C++17 Using for_each() FunctionConclusionReferences Maps in C++ store the data in form of key and […]

  • How to write binary files in C++
    14 March

    Write Binary Files in C++

    Table of ContentsOpening a File in Binary Mode Using fstream LibraryWriting the Binary File Using the write() Function of fstream LibraryWriting the Binary File Using the put() Function of fstream LibraryWriting the Binary File Using Insertion Operator (<<) Of fstream LibraryWriting the Structure Object to Binary File Using fstream LibraryConclusion In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.