Convert String to Char Array in C++
In C++, we usually represent text data using the std::string object. But in some cases, we may need to convert a std::string to a character array, the traditional C-style strings. In this article, we will learn how to convert the string to char array in C++.
Examples
Input: str = "geeksforgeeks"
Output: char arr[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's', '\0' };
Explanation: str is converted to character array.Input: str = "abc"
Output: char arr[] = {'a', 'b', 'c', '\0'}
Explanation: str is converted to character array.
Following are the different ways to convert the string to char array in C++:
Using std::string::c_str() Method
To change std::string to char array, we can first use string::c_str() function to get the underlying character array that contains the string stored in std::string object. Then we can create a copy of this char array using strcpy() function. We can also use & (Address-of) operator to get the underlying character array by fetching the address of the first character.
Code Implementation
// C++ program to convert string to char array
// using string::c_str()
#include <bits/stdc++.h>
using namespace std;
int main() {
// Assigning value to string s
string str = "geeksforgeeks";
int n = str.length();
// declaring character array (+1 for null
// character)
char arr[n + 1];
// copying the contents of the string to
// char array
strcpy(arr, str.c_str());
cout << "{ ";
for (int i = 0; i < n; i++)
cout << arr[i] << ", ";
cout << "}";
return 0;
}
Output
{ g, e, e, k, s, f, o, r, g, e, e, k, s, }
Time complexity: O(n), where n is the number of characters in a string.
Auxiliary Space: O(n)
Using std::copy() Method
In the above method, we have first fetched the underlying character array of std::string object. But we can also directly convert std::string to char array by using std::copy() function. This function works for both iterators and pointers so we can copy the std::string objects (uses iterator) directly to character array (uses pointers).
Syntax
copy(first, last, pos);
Parameters
- first: Iterator to the beginning of string object.
- last: Iterator to the position just after the end of the string object.
- pos: Pointer to the start of the character array
Code Implementation
// C++ Program for converting the string to
// char array using std::copy()
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "geeksforgeeks";
int n = str.length();
char arr[n + 1];
// Specify the ranges
auto first = str.begin();
auto last = str.end();
// Convert the string to char array
copy(first, last, arr);
// Null terminate the char array
arr[n] = '\0';
cout << "{ ";
for (int i = 0; i < n; i++)
cout << arr[i] << ", ";
cout << "}";
return 0;
}
Output
{ g, e, e, k, s, f, o, r, g, e, e, k, s, }
Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(n)
Using Loops
To manually covert the string to char array, we can use any C++ loop to iterate through each element of the std::string and copy the character to the char array one by one.
Code Implementation
// C++ program to manually convert string to
// char array using a loop
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "geeksforgeeks";
int n = str.length();
// Create a new array of chars to copy the
// string
char arr[n + 1];
// Null terminate char array
arr[n] = '\0';
// Copy each character of str to arr
for (int i = 0; i < n; i++)
arr[i] = str[i];
cout << "{ ";
for (int i = 0; i < n; i++)
cout << arr[i] << ", ";
cout << "}";
return 0;
}
Output
{ g, e, e, k, s, f, o, r, g, e, e, k, s, }
Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(n)