Run ID:118427

提交时间:2025-04-30 21:04:12

#include <bits/stdc++.h> using namespace std; char s[1005]; /*我们定义一个数组大小最好 原基础上 + 5 Hello LEMA 每一个单词后面有空格 gets(s); -> s[strlen(s)] = ' '; -> strlen(s) -> char数组中求长度的一个函数 "hello lema" -> "hello lema "*/ int main() { gets(s); s[strlen(s)] = ' '; int start = 0; for(int i = 0; i < strlen(s); ++i) //s.size() -> string { if(s[i] == ' '){//-> 前一个位置是这个单词的最后一个字母 -> 终点 reverse(s + start, s + i); //-> 实际反转的范围是start -> i - 1 //start要更新,因为你已经操作完这个单词了,你要看下一个单词 //空格后面-> i -> i + 1; start = i + 1; } } cout << s << endl; /*string: reverse(s.begin(), s.end()); char 数组: reverse(s + start, s + i); /*string s; getline(cin, s); reverse(s.begin(), s.end()); cout << s << endl; /*error: 'reverse' was not declared in this scope 错误: 反转函数 -> 你没有去写这一个头文件 -> 手机APP 是不是要开通会员 ->*/ return 0; }