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

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

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

#include <stdio.h>
#include "string.h"
int main() 
{
    char str[10001];
    fgets(str,sizeof(str),stdin);
    int ans =0 ;
    int n = strlen(str);
    if(n > 0 &&str[n-1] == '\n')
    {
            str[n-1] = '\0';
    }
    for(int i = 0 ; str[i] !='\0'; i++)
    {
        if(str[i] != ' ')
        {
            ans++;
        }
        else 
        {
            if (str[i+1] != '\0') 
            ans = 0;
        }
    }
    printf("%d",ans);
    return 0;
}

全部评论

相关推荐

肥肠椒绿:双非本可不就犯天条了,双非本就应该打入无间地狱
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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