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

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

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


#include<stdio.h>
#include<stdlib.h>                   //malloc函数
#include<string.h>                   //strlen函数

int main(){
    
    char* p = (char*)malloc(sizeof(char));
    int cnt = 0;
    
    gets(p);
    char* q = p + strlen(p) - 1;     //从串尾开始计数
    for( ; q>=p; q--){               //必须取等,考虑只有一个单词的情况
        if(*q == ' ')
            break;
        cnt++;
    }
    
    printf("%d", cnt);
    free(p);
    
    return 0;
}


全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务