输入一行字符,统计其中有多少个单词,单词之间用空格分开。

代码:

#include<stdio.h>
int main()
{
    char a[100],c;
    int i,word=0,sum=0;
    gets(a);
    for(i=0;(c=a[i])!='\0';i++)
    {
        if(c==' ') word=0;
        else if(word==0)
            {
            word=1;
            sum++;
            }
    }
    printf("%d",sum);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/blacksleeping/article/details/81238516