例题描述
对字符串中的所有单词进行倒排。
说明:
-
每个单词是以
26
个大写或小写英文字母构成; -
非构成单词的字符均视为单词间隔符;
-
要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;
-
每个单词最长
20
个字母;
-
输入描述:
输入一行以空格来分隔的句子。 -
输出描述:
输出句子的逆序。
示例1:
- 输入:
I am a student
- 输出:
student a am I
以前的思路不能解决该问题
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string str;
while (getline(cin, str)) {
std::reverse(str.begin(), str.end());
for (int i = 0; i < (int)str.size(); ++i) {
if (isspace(str[i])) continue;
int index = i++;
while (!isspace(str[i]) && i < (int