题解 | #句子逆序#
句子逆序
https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D2%26tpId%3D37%26type%3D37
/* 思路: 利用 reverse 对字符串进行翻转 1. 获得输入的字符串 2. 调用reverse 整体翻转字符串 3. 加控制逻辑, 识别空格,翻转 翻转后的字符串 控制逻辑这块要多练一练 4. 打印输出 */ #include <algorithm> #include<iostream> using namespace std; int main(){ string str; getline(cin, str); reverse(str.begin(), str.end()); int len = str.length(); for(int i = 0; i < len; ++i){ int j = i; while(str[j] != ' ' && j < len){ ++j; } reverse( str.begin() + i, str.begin() + j); i = j; } cout << str << endl; }