题目要求:
将一个字符串str的内容颠倒过来,并输出。str的长度不超过100个字符。
输入:
从键盘接收的任意一行字符串。
输出:
转换好的逆序字符串。
示例输入:
I am a student
示例输出:
tneduts a ma I
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
getline(cin,a);
string b(a.rbegin(),a.rend());
cout<<b;
return 0;
}
getline(cin,a);
string b(a.rbegin(),a.rend());
不好意思,以上两句能简单解释下吗。。。不是很懂getline以及迭代器的用法。。C++初学者。。
看下http://www.cplusplus.com/reference/
里面的string api
当输入的字符串含有空格时,输出不对
追答因为有空格被cin识别为分割符。
支持空格的代码如下:
#include <string>