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

#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 != '
')    //输入一串英文
    {
            ch = getchar();
            str[j]=ch;
            j++;
    }
    str[j]='';    
    
    while(str[i]!='')    //统计单词个数
    {
            if(str[i]==' '&&str[i+1]!=' '&&str[i+1]!='')
            count ++; 
            i++;
    }
    printf("%d
",count);
}

测试结果:

I am going to shoping!
5


------------------
(program exited with code: 0)
Press return to continue
原文地址:https://www.cnblogs.com/wystan/p/4556869.html