#include <stdio.h>
#define IN 1
#define OUT 0
int main()
{
int nc,nw;
int c;
nw=0;nc=0;
while((c=getchar())!=EOF)
{
++nc;
if(c==' '||c==' \n'||c=='\t‘)
state = OUT;
else if(state=OUT)
{
count++;
state = IN;
}
printf(“%d %d %d”,“nw,nc”);
return 0;
}
2、在理解上个例子的基础上,及理解状态state变化的思想。编写一个程序,使其以每行一个单词进行输出。
程序如下:
#include<stdio.h>
#define OUT 0
int main()
{
int c;
int nw=0;
while((c=getchar())!=EOF)
{
if(c==' '||c=='\t'||c=='\n’)
{
if(state==IN) //the end of word
{
printf(“\n”);
state=OUT;
}
}
else if(state==OUT)
{
putchar(c);
state = IN;
}
else
putchar(c);
}
}