在 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 红包祝福语简短10字
18 周六早晨祝福语简短