+ 1

How to change string to char of array?

23rd Aug 2016, 3:36 AM
Edison Ho
Edison Ho - avatar
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.
23rd Aug 2016, 5:49 AM
Donovan
Donovan - avatar
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.
23rd Aug 2016, 9:37 AM
Stefan
Stefan - avatar
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>
23rd Aug 2016, 3:25 PM
kiwiyou
kiwiyou - avatar