+ 1
How to change string to char of array?
3 Answers
0
Go through the string with a for statement.
vector<char>vect_name{};
for ( int z = 0; z<string.size(); z++)
{
char name;
char name = string[z];
vect_name.push_back(name)
}
You may have to adjust this code, and the last time I did this ( I used this to take a string, break it into a vector of chars, then reverse and reassemble it into a string.) I assembled it using a compiler flagged to compile C++14 code.
0
You could just use the c_str() function on the string object. It returns a char*. I would assume that the char* is actually const since it directly refers to the buffer of the string object.
0
char* array = nullptr;
string s = "test string";
array = new char[s.length()];
strcpy(array, s.c_str());
cout << array << endl;
strcpy is in <cstring>