c++中如何将输入的字符串(不知长度)存入数组中?

2024-11-22 13:16:29
推荐回答(1个)
回答1:

#include
#include
#include
using namespace std;
int main()
{
string str;
char temp;
cout<<"输入字符串,按回车键结束输入"<while((temp=cin.get())!='\n')
{
str +=temp;
}
const int LEN =str.length();
char* dest = new char[LEN];//根据输入字符串的长度,创建字符数组
for(int i = 0;i{
dest[i]=str[i];
cout<}
delete dest;
system("pause");
return 0;
}