小编Blu*_*uuu的帖子

计算最长单词中的字母数

我想得到最长字的字母数.如果我输入像"你好我"这样的东西,我会得到5分,但如果我写一些更长的东西,比如"英雄联盟",我会得到6而不是7.为什么?

#include <stdio.h>
int longest_word(const char string[]){
    int i;
    int max;
    int cont;
    i=0;
    while(string[i]!='\0'){
        for(cont=0;string[i]!=' '&& string[i]!='\0';i++)
            cont++;
        if (cont>max)
            max=cont;
        ++i;
    }
    return max;
}
int main(void){
    char f[100];    #maybe this is the problem?
    int i;
    printf("input a string: ");
    scanf("%s",f);
    i=longest_word(f);
    printf("%d",i);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
471
查看次数

标签 统计

c ×1