在 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 兄妹拍照祝福语简短的话