输入一行统计其中单词的个数

#include <string.h>
#include <stdio.h>
int main()
{
    int count = 1;
    char str[1024];
    int i=0;
    int j =0;
    char ch='0';
    while(ch != '\n')    //输入一串英文
    {
            ch = getchar();
            str[j]=ch;
            j++;
    }
    str[j]='\0';    
    
    while(str[i]!='\0')    //统计单词个数
    {
            if(str[i]==' '&&str[i+1]!=' '&&str[i+1]!='\0')
            count ++; 
            i++;
    }
    printf("%d\n",count);
}

测试结果:

I am going to shoping!
5


------------------
(program exited with code: 0)
Press return to continue

 

posted @ 2015-06-06 16:42  道心不可练  阅读(537)  评论(0编辑  收藏  举报