Kodewick
開始學 →
小挑戰←→0 / 70

最長的詞

#065小挑戰約 6 分鐘

明信片的格子很小,先看看最長的詞有多長。寫 longest_word(s),返回 s 裡最長那個詞有幾個字母。詞之間是空格(可能不止一個);空字串返回 0。

示例
longest_word("see you at the beach")→5
longest_word("")→0
函式int longest_word(const char *s)
main.c
int longest_word(const char *s) {
    int now = 0;
    int best = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        // 在這裡寫:遇到空格 now 歸零;不然 now 加 1,比 best 大就記下來
    }
    return best;
}
Ctrl + Enter 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。