1. 程式人生 > >C語言實現計算一個檔案中的單詞個數

C語言實現計算一個檔案中的單詞個數

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);

}