c语言程序设计_单词计数

代码如下
#include <stdio.h>

#define IN 1
#define OUT 0

main()
{
	int c,nl,nw,nc,state;  /*nl行数,nw单词数,nc字符数*/

	state=OUT;
	nl=nc=nw=0;
	while((c = getchar()) != EOF)
	{
		++nc;
		if(c == '\n')
			++nl;
		if(c == ' ' || c == '\n' || c == '\t')
			state = OUT;
		else if(state == OUT)
		{
			state = IN;
			++nw;
		}
	}
	printf("%d %d %d\n", nl, nw, nc);
}

练习1-11如何测试单词计数程序?如果程序中存在某种错误,那么什么样的输入最可能发现这类错误呢?

练习1-12 编写一个程序,以每行一个单词的形式打印其输入。

        思路:当遇到空格,换行符,制表符,输出'\n'。

注:EOF 在windows下 Ctrl+z  , 在Unix下 Ctrl+d 。

猜你喜欢

转载自blog.csdn.net/birdunderastarrysky/article/details/80961177