题解 | 句子逆序
句子逆序
https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3
#include <stdio.h>
#include <string.h>
int main() {
char str[1001];
int count = 0;
char *words[500];
fgets(str, 1000, stdin);
str[strcspn(str, "\n")] = '\0'; // 去除换行符
char *p = str;
while(*p)
{
while(*p == ' ') p++; //处理连续空格的情况
if(*p == '\0') break; //在while循环后执行 涵盖到只含多个空格的情况
words[count ++] = p; //记录单词的起始位置;
while(*p != ' ' && *p != '\0') p++;
*p = '\0';
p++;
}
for(int i = count-1; i >= 0; i--) {
printf("%s ", words[i]); // 逆序输出
}
return 0;
}

