在 C++ 中将字符串转换为字符数组
这是一个C++程序,用于将字符串转换为C++中的字符数组。这可以通过多种方式完成:
类型1:
算法
Begin
Assign value to string m.
For i = 0 to sizeof(m)
Print the char array.
End示例代码
#include#include using namespace std; int main() { char m[] = "Nhooo"; string str; int i; for(i=0;i 类型2:
我们可以简单地调用strcpy()函数将字符串复制到字符数组。
算法
Begin Assign value to string s. Copying the contents of the string to char array using strcpy(). End示例代码
#include#include #include using namespace std; int main() { string str = "Nhooo"; char c[str.size() + 1]; strcpy(c, str.c_str()); cout << c << '\n'; return 0; } 输出
Nhooo类型3:
我们可以避免使用strcpy()基本上在c中使用的
std::string::copy instead.算法
Begin Assign value to string s. copying the contents of the string to char array using copy(). End示例代码
#include#include using namespace std; int main() { string str = "Nhooo"; char c[str.size() + 1]; str.copy(c, str.size() + 1); c[str.size()] = '\0'; cout << c << '\n'; return 0; } 输出
Nhooo
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短