题解 | 句子逆序

句子逆序

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;
}

全部评论

相关推荐

牛至超人:把哈工大,再加大加粗,看见闪闪发光的哈工大字样,面试官直接流口水
投递字节跳动等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务