Run ID:114130
提交时间:2025-03-21 22:25:52
#include <iostream> #include <string> using namespace std; int main() { string s, rs; getline(cin, s); int end = s.size()-1; while (end >= 0) { while (end >=0 && s[end] == ' ') end--; // 跳过末尾空格 if (end < 0) break; int start = end; while (start >=0 && s[start]!=' ') start--; // 找到单词起点 // 手动拼接字符 (替代 substr) for (int i=start+1; i<=end; i++) rs += s[i]; rs += " "; // 添加单词间空格 end = start-1; // 继续处理前一个单词 } if (!rs.empty()) rs.pop_back(); // 删除末尾空格 cout << rs; }