酷代码 AI
菜单
服务商

C++ 实现:轻松反转字符串中单词顺序,解决空格难题!

```cpp #include <iostream> #include <cstdlib> #include <string> using namespace std; string reverseWords(string s) { string result; int right = s.length() - 1; while (right >= 0) { while (right >= 0 && s[right] == ' ') { right--; } if (right < 0) break; int left = right; while (left >= 0 && s[left] != ' ') { left--; } result += s.substr(left + 1, right - left); result += " "; right = left; } if (!result.empty()) { result.pop_back(); } return result; } ``` ######[AI问答 | 183点数解答 | 2025-11-12 14:50:49]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]