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

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

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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int main(void) {
    int getLength( char* string);
    char* string = (char*)malloc(sizeof(char) * 5000);
    scanf("%[^\n]", string);
    int length = getLength(string);
    printf("%d\n", length);
    return 0;
}
int getLength(char* string) {
    int counter = 0;
    int length = strlen(string);
    for (int i = length - 1; i >= 0; i--) {     
        char character = string[i];
        if (isspace(character)) break;
        counter++;
    }
    return counter;
}

全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
05-14 18:44
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务