C语言实现计算一个文件中的单词个数

时间:2021-05-14 03:40:43
void count_world()
{
	int nl, nc, nw, state, c;
	nl = nc = nw = 0;
	FILE *fp;
	state = OUT;
	fp = fopen("./a.txt","r");

	while((c = fgetc(fp)) != EOF)
	{
		DEBUG("c:%c\n",c);
		nc++;
		if(c == '\n')
			nl++;
		if (  (' ' == c )  ||  ('\t' == c )   ||  ('\n' == c )   )
		{
			DEBUG2("in the out the world");
			state = OUT;
		}
		else if (OUT == state )
		{
			DEBUG2("in the world");
			state = IN;
			nw++;
		}

	}
	DEBUG("nl=%d\nnc=%d\nnw=%d\n",nl,nc,nw);

}