Kodewick
开始学 →
小挑战←→0 / 70

日记有几个词

#068小挑战字符串条件约 6 分钟

英文日记要写够词数。写 count_words(s),数 s 里有几个词。词之间是空格,可能不止一个,开头结尾也可能有空格;空字符串返回 0。

示例
count_words("went to the zoo")→4
count_words("")→0
函数int count_words(const char *s)
main.c
int count_words(const char *s) {
    int words = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        // 在这里写:这个字符不是空格,而且是第一个或前面是空格,就是一个新词
    }
    return words;
}
Ctrl + Enter 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。