代码
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
//将字符数组中的数据传入字符串中
char test[50] = {};
gets_s(test);//用cin无法识别空格
string testString = test;
cout << testString<<endl;
//将字符串中的数据传入字符数组中
char test2[50];
for (int i = 0;i < strlen(test);i++)
{
test2[i] = testString[i];
cout << test2[i];
}
}
运行结果

该程序演示了如何在C++中将字符数组的数据转换为字符串,以及如何将字符串的数据转换回字符数组。使用`gets_s`函数读取字符数组,因为空格问题,`cin`在此场景下不适用。然后,通过循环遍历字符串长度,逐个字符复制到新的字符数组中。
7432





