c++ 设计一回圈程式.输入一字串符后,程式即将字串反转

来源:百度知道 编辑:UC知道 时间:2024/06/01 21:33:59
请设计一回圈程式,此程式会不断要求使用者输入一字串(字元数不超过
20),直到输入值为0 才结束程式。每当输入完一字串后,程式即将字串反
序并输出至萤幕上。
范例如下:

Please input a string: 0aBcdEfg
The inverse string is gfEdcBa0.

Please input a string: abc
The inverse string is cba.

Please input a string: 0
See You Next Time! Bye Bye!

请按任意键继续…

#include <iostream>
#include <string>

using namespace std;

int main()
{
string str;
while(1)
{
cout<<"Please input a string:";
cin>>str;
if(str == "0")
{
break;
}
cout<<"The inverse string is:";
for(int i=str.size()-1;i>=0;i--)
{
cout<<str[i];
}
cout<<endl;
}
return 0;
}

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
string str;
while(1)
{
cout<<"Please input a string:";
cin>>str;
if(str == "0")
{
break;
}
cout<<"The inverse string is:";
reverse( str.begin() , str.end() ) ;
cout << str << endl ;

}