是否有一些函数可以在 C++ 中将字符串转换为数组
is There some function that make a conversion from string to array in C++
string x = "Banana";
如何将其转换为这样的字符:
char x[]={'B', 'a', 'n', 'a', 'n', 'a'};
在c++中可以按照这种方式将字符串转换为字符数组。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
// assigning value to string s
string s = "Banana";
int n = s.length();
// declaring character array
char char_array[n + 1];
// copying the contents of the
// string to char array
strcpy(char_array, s.c_str());
// this part need to be deleted. This is only for verification.
for (int i = 0; i < n; i++)
cout << char_array[i] << endl;
return 0;
}
string x = "Banana";
如何将其转换为这样的字符:
char x[]={'B', 'a', 'n', 'a', 'n', 'a'};
在c++中可以按照这种方式将字符串转换为字符数组。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
// assigning value to string s
string s = "Banana";
int n = s.length();
// declaring character array
char char_array[n + 1];
// copying the contents of the
// string to char array
strcpy(char_array, s.c_str());
// this part need to be deleted. This is only for verification.
for (int i = 0; i < n; i++)
cout << char_array[i] << endl;
return 0;
}