时间限制: 1 Sec 内存限制: 128 MB
    题目描述
    本题目要求编写程序统计一行字符中单词的个数。所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个。
    输入
    输入给出一行字符。
    输出
    在一行中输出单词个数。
    样例输入
    Let’s go to room 209.
    样例输出
    5

    1. #include <stdio.h>
    2. int main()
    3. {
    4. int count = 0, i = 0;
    5. char str[1000];
    6. gets(str); //输入字符
    7. while (str[i] == ' ') //跳过开头的空格
    8. i++;
    9. while (str[i] != '\0') //循环到\0时结束
    10. {
    11. if (str[i] != ' ')
    12. {
    13. count++;
    14. while (str[i] != ' ') //跳过一个单词的其他字符
    15. {
    16. if (str[i] == '\0') //如果循环到\0,跳出循环
    17. break;
    18. i++;
    19. }
    20. }
    21. else
    22. {
    23. while (str[i] == ' ')
    24. i++;
    25. }
    26. }
    27. printf("%d\n", count);
    28. return 0;
    29. }