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

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

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

全部评论

相关推荐

02-05 08:18
四川大学 Java
在思考的熊熊很讨厌吃香菜:不是,我门头沟学院呢?这都没排上?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务