NowCode:HJ12 字符串反转

题目: 字符串反转

描述

接受一个只包含小写字母的字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)

输入描述:

输入一行,为一个只包含小写字母的字符串。

输出描述:

输出该字符串反转后的字符串。

示例

1
2
3
4
5
输入:
abcd

输出:
dcba

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
string input;
getline(cin, input);
auto first = input.begin();
auto last = input.end();
while(first != last && first != --last) {
std::iter_swap(first, last);
first++;
}
cout << input;
}

思路

与上一题基本一样,故不用一分钟就能做出来

此题简单