在 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 姐妹分手了祝福语简短