在 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
热门推荐
3 带马的祝福语简短
10 简短朴实的生日祝福语
11 升职祝福语简短霸气押韵
12 清新唯美生日祝福语简短
13 小区婚礼祝福语简短大气
14 元旦祝福语同学简短创意
15 靳年祝福语大全简短
16 早安朋友祝福语简短独特
17 对战友的祝福语简短
18 幼儿转园祝福语简短