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

  • Get Number of Elements in Array in C++
    08 October

    Get Number of Elements in Array in C++

    Table of ContentsGet Number of Elements in Array in C++Using the sizeof() functionUsing the pointersUsing the size() functionGet the frequency of each element in array in C++Using nested for loopUsing maps One of the most fundamental collections in any programming language is an array. It stores a similar type of data at specific indices that […]

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

  • Enum to String in C++
    19 July

    Convert enum to string in C++

    Table of ContentsUsing stringify macro method to convert enum to String in C++Using const char* Array to convert enum to String in C++Using a custom-defined function to convert enum to String in C++Conclusion There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

  • String contains C++
    07 June

    Check if string contains substring in C++

    Table of ContentsUsing find() function to check if string contains substring in C++.Using strstr() method to check if string contains substring in C++Using boost library to check if string contains substring in C++Using custom functions to check if string contains substring in C++Conclusion There are different ways to check if string contains substring in C++. […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.