状态机实现文件单词统计


状态机实现文件单词统计


原理:
数据结构:
在这里插入图片描述
参考:

#include <stdio.h>
#define OUT	0
#define IN	1

#define INIT 	OUT

int splite(char c) {
	if ((' ' == c) || ('\n' == c) || ('\t' == c) ||
			('\"' == c) || ('\'' == c) || ('+' == c) ||
			(',' == c))
		return 1;
	else
		return 0;
}

int count_word(char *filename) {

	int status = INIT;
	int word = 0;

	FILE *fp = fopen(filename, "r");
	if (fp == NULL) return -1;

	char c;
	while ((c = fgetc(fp)) != EOF) {

		if (splite(c)) {
			status = OUT;
		}else if(OUT == status){
			status = IN;
			word ++;
		}
		
	}

	return word;
}

int main(int argc, char *argv[]) {

	if (argc < 2) return -1;
	printf("about word: %d\n", count_word(argv[1]));

	return 0;
}

运行结果:
在这里插入图片描述
英语单词(随便找的):
Book III Lesson 1
A puma at largePumas are large, cat-like animals which are found in America.
When reports came into London Zoo that a wild puma had been spotted forty-five miles south of London, they were not taken seriously.
However, as the evidence began to accumulate, experts from the Zoo felt obliged to investigate, for the descriptions given by people who claimed to have seen the puma were extraordinarily similar.
The hunt for the puma began in a small village where a woman picking blackberries saw ‘a large cat’ only five yards away from her.
It immediately ran away when she saw it, and experts confirmed that a puma will not attack a human being unless it is cornered.
The search proved difficult, for the puma was often observed at one place in the morning and at another place twenty miles away in the evening.
Wherever it went, it left behind it a trail of dead deer and small animals like rabbits.
Paw prints were seen in a number of places and puma fur was found clinging to bushes.
Several people complained of ‘cat-like noises’ at night and a businessman on a fishing trip saw the puma up a tree.

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weifc-wei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值