1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-12-16 15:44:39 +00:00
202401-programming-assignments/【实践课外】9.数组3/7-2 说反话-加强版.c

44 lines
834 B
C

#include <stdio.h>
#include <string.h>
char s[500005];
int cnt, start_pos[500005], len[500005];
int main() {
gets(s);
int len_s = strlen(s);
for (int i = 0; i < len_s; i++) {
if (s[i] == ' ') {
if (i == 0 || s[i - 1] == ' ') {
continue;
}
start_pos[cnt] = i - len[cnt];
cnt++;
len[cnt] = 0;
} else {
len[cnt]++;
}
}
if (len_s > 0 && s[len_s - 1] != ' ') {
start_pos[cnt] = len_s - len[cnt];
cnt++;
}
for (int i = cnt - 1; i >= 0; i--) {
for (int j = start_pos[i]; j < start_pos[i] + len[i]; j++) {
printf("%c", s[j]);
}
if (i != 0) {
printf(" ");
} else {
printf("\n");
}
}
return 0;
}