Here, we will build a C++ program to convert strings to char arrays. Many of us have encountered the error ‘cannot convert std::string to char[] or char* data type’ so let’s solve this using 5 different methods:
- Using c_str() with strcpy()
- Using c_str() without strcpy()
- Using for loop
- Using the address assignment of each other method
- Using data() (C++17 and newer)
Input:
string s = "geeksforgeeks";
Output:
char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's', '\0' };
1. Using c_str() with strcpy()
A way to do this is to copy the contents of the string to the char array. This can be done with the help of the c_str() and strcpy() functions of library cstring.
The c_str() function is used to return a pointer to an array that contains a null-terminated sequence of characters representing the current value of the string.
const char* c_str() const;
If there is an exception thrown then there are no changes in the string. But when we need to find or access the individual elements then we copy it to a char array using strcpy() function. After copying it, we can use it just like a simple array. The length of the char array taken should not be less than the length of an input string.
In order to create a new array to contain the characters, we must dynamically allocate the char array with new. We also must remember to use delete[] when we are done with the array. This is done because, unlike C, C++ does not support Variable Length Arrays (VLA) on the stack.
Example:
C++
#include <cstring>
#include <string>
#include <iostream>
int main()
{
std::string s = "geeksforgeeks" ;
const int length = s.length();
char * char_array = new char [length + 1];
strcpy (char_array, s.c_str());
for ( int i = 0; i < length; i++)
{
std::cout << char_array[i];
}
delete [] char_array;
return 0;
}
|
- Time complexity: O(n)
- Auxiliary Space: O(1)
2. Using c_str() without strcpy()
An alternate way of Method 1 can be such, without using strcpy() function. This option will not create a new string.
Example:
C++
#include <cstring>
#include <string>
#include <iostream>
int main()
{
std::string s = "GeeksForGeeks" ;
const char * str = s.c_str();
std::cout << str;
return 0;
}
|
- Time complexity: O(n)
- Auxiliary Space: O(1)
3. Using for loop
We can use for loop to iterate through each element of the std::string and assign the character to the char array one by one.
Example:
C++
#include <iostream>
#include <string>
int main()
{
std::string s = "GeeksForGeeks" ;
char * char_array = new char [s.length() + 1];
char_array[s.length()] = '\0' ;
for ( int i = 0; i < s.length(); i++) {
char_array[i] = s[i];
}
std::cout << char_array;
delete [] char_array;
return 0;
}
|
- Time complexity: O(n)
- Auxiliary Space: O(1)
4. Using the address assignment of each other method
This is the simplest and most efficient one. We can directly assign the address of 1st character of the string to a pointer to the char. This should be the preferred method unless your logic needs a copy of the string.
Example:
C++
#include <iostream>
#include <string>
int main()
{
std::string s = "GeeksForGeeks" ;
char * char_arr = &s[0];
std::cout << char_arr;
return 0;
}
|
- Time complexity: O(n)
- Auxiliary Space: O(1)
5. Using .data() (with C++17 or newer)
Using .data() to access a non-const char* of the std::string is the best option in C++17 or newer. Note: This can’t be run in the GeeksforGeeks IDE because it doesn’t support C++17.
Example:
C++
#include <iostream>
#include <string>
int main()
{
std::string s = "GeeksForGeeks" ;
char * char_arr = s.data();
std::cout << char_arr;
return 0;
}
|
Output
GeeksForGeeks
- Time complexity: O(n)
- Auxiliary Space: O(1)