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

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

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

#include <stdio.h>
#include"string.h"
int main() {
    int a=0, b=-1;
    char str[5000];
    gets( str);
    while (str[a]!='\0') { // 注意 while 处理多个 case
        // 64 位输出请用 printf("%lld") to 
        if(str[a]==' ')
        {
            b=a;
        }
        a++;
        
        
    }
    printf("%d\n", strlen(str)-b-1);
    return 0;
}

读取最后一个空格的下标,然后通过strlen函数读取字符串的长度,两者相减再稍加调试即可

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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