题解 | #字符串最后一个单词的长度#

字符串最后一个单词的长度

https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da

#include <stdio.h>
#include <string.h>
#include <stddef.h>
int main() {
    char str[5002];
    while (fgets(str, sizeof(str), stdin) != NULL) {
        str[strcspn(str,"\n")] = '\0';
        int lengthofstr = strlen(str);
        char *last_space = strrchr(str, ' ');
        if(last_space != NULL) {
            ptrdiff_t lastwordLength = (char *)str + lengthofstr - last_space - 1;
            printf("%td", lastwordLength);
        } else {
            size_t lastwordLength = strlen(str);
            printf("%zu", lastwordLength);
        }
        
    }
    return 0;
}

全部评论

相关推荐

11-11 16:40
已编辑
门头沟学院 人工智能
不知道怎么取名字_:这个有点不合理了,相当于已经毕业了,但还是没转正,这不就是白嫖
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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