How to convert a string to char array in C++?

In this article, we will discuss different ways to convert a string to a char array in C++.

Table Of Contents

Problem Description

We have given a string and we have to convert the string to a char array.

Input

string s = "Hello World";

Output

Advertisements
Hello World

We can convert a string to a char array using different techniques. Let’s discuss all these methods one by one.

Convert string to char array using strcpy() function

In this approach, the string is copied into the char array with the help of strcpy() function.

Time Complexity: O(n)
Space Complexity: O(1)

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    // declare the string
    string strValue = "Hello World";

    // declare the char array with capacity
    // one more than the string size
    char chArr[strValue.size() + 1];

    // apply strcpy function for copy
    // the string in char array
    strcpy(chArr, strValue.c_str());

    // print the char array
    cout << chArr << endl;

    return 0;
}

Output

Hello World

In this example, we created a char array from a string object in C++.

Convert string to char array using string::copy() function

In this approach, the string is copied in the char array with the help of copy() member function of string class.

Time Complexity: O(n)
Space Complexity: O(1)

#include <iostream>

using namespace std;

int main()
{
    // declare the string
    string s = "Hello World";

    // declare the char array with capacity
    // one more than the string size
    char cstr[s.size() + 1];

    // apply string::copy function for copy
    // the string in char array
    s.copy(cstr, s.size() + 1);

    // assign '
#include <iostream>
using namespace std;
int main()
{
// declare the string
string s = "Hello World";
// declare the char array with capacity
// one more than the string size
char cstr[s.size() + 1];
// apply string::copy function for copy
// the string in char array
s.copy(cstr, s.size() + 1);
// assign '\0' at the end of char array
cstr[s.size()] = '\0';
// print the char array
cout << cstr <<endl;
return 0;
}
' at the end of char array cstr[s.size()] = '
#include <iostream>
using namespace std;
int main()
{
// declare the string
string s = "Hello World";
// declare the char array with capacity
// one more than the string size
char cstr[s.size() + 1];
// apply string::copy function for copy
// the string in char array
s.copy(cstr, s.size() + 1);
// assign '\0' at the end of char array
cstr[s.size()] = '\0';
// print the char array
cout << cstr <<endl;
return 0;
}
'; // print the char array cout << cstr <<endl; return 0; }

Output

Hello World

In this example, we created a char array from a string object in C++.

Convert string to char array using STL’s copy() function

In this approach, the string is copied in the char array with the help of copy() function of STL.

Time Complexity: O(n)
Space Complexity: O(1)

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    // declare the string
    string s = "Hello World";

    // declare the char array with capacity
    // one more than the string size
    char cstr[s.size() + 1];

    // apply copy function for copy the string
    // in char array
    copy(s.begin(), s.end(), cstr);

    // assign '
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
// declare the string
string s = "Hello World";
// declare the char array with capacity
// one more than the string size
char cstr[s.size() + 1];
// apply copy function for copy the string
// in char array
copy(s.begin(), s.end(), cstr);
// assign '\0' at the end of char array
cstr[s.size()] = '\0';
// print the char array
cout << cstr << endl;
return 0;
}
' at the end of char array cstr[s.size()] = '
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
// declare the string
string s = "Hello World";
// declare the char array with capacity
// one more than the string size
char cstr[s.size() + 1];
// apply copy function for copy the string
// in char array
copy(s.begin(), s.end(), cstr);
// assign '\0' at the end of char array
cstr[s.size()] = '\0';
// print the char array
cout << cstr << endl;
return 0;
}
'; // print the char array cout << cstr << endl; return 0; }

Output

Hello World

In this example, we created a char array from a string object in C++.

Summary

In this article, we have seen various methods for converting a string to a char array with the use of STL functions and string’s member functions. Each method has its own time and space complexity. Happy Learning.

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top