统计单词个数
输入一行字符,统计其中单词个数.各单词之间用空格分隔,空格数可以是多个
#include <stdio.h>
#define IN 1 // 在单词内
#define OUT 0 //在单词外
int main(void)
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != '\n')
{
if (c == ' ' || c== '\t')
{
++nl;
state = OUT;
}
else if (state == OUT)
{
state = IN;
++nw;
}
}
printf ("%d %d\n", nl, nw);
return 0;
}