需求是给出一个文本文件,求出其中有多少个单词(不用判断单词是否为真的英语单词)。
分析:因为不需要判断是否为真的英语单词,这就简单多了。只要以空格,TAB,标点符号等非字母间隔的字符串都是单词(不考虑连字符-)。我的解决方法是判断一个单词,只要满足一个是字母的字符后一个字符不是字母,即可算是一个单词。程序如下:
系统IO方式:
/************************************************************************* > File Name: WordCount_SYSIO.c > Author: Baniel Gao > Mail: createchance@163.com > Blog: blog.csdn.net/createchance > Created Time: Fri 11 Apr 2014 04:47:07 PM CST ************************************************************************/ #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #define BUFF_SIZE 1024 int is_letter(char ch); int main(int argc, char *argv[]) { int fd, cnt = 0; int ret, i = 0; char buff[BUFF_SIZE]; if (argc != 2) { printf("Usage: %s filename \n", argv[0]); return -1; } if (-1 == (fd = open(argv[1], O_RDONLY))) { perror("open file"); return -1; } while (1) { ret = read(fd, buff, BUFF_SIZE); if (ret == -1) return -1; else if (ret == 0) break; for (i = 0; i < strlen(buff) - 1; i++) { if (is_letter(buff[i]) && (!is_letter(buff[i + 1]))) cnt++; } } printf("words: %d \n", cnt); return 0; } int is_letter(char ch) { if ((ch >= ‘a‘ && ch <= ‘z‘) || (ch >= ‘A‘ && ch <= ‘Z‘)) return 1; else return 0; }
/************************************************************************* > File Name: WordCount_STDIO.c > Author: Baniel Gao > Mail: createchance@163.com > Blog: blog.csdn.net/createchance > Created Time: Sat 12 Apr 2014 02:39:16 PM CST ************************************************************************/ #include <stdio.h> #include <string.h> #define BUFF_SIZE 1024 int is_letter(char ch); int main(int argc, char *argv[]) { FILE *fp; char buff[BUFF_SIZE]; int i = 0, cnt = 0; if (argc != 2) { printf("Usage: %s filename \n", argv[0]); return -1; } if (NULL == (fp = fopen(argv[1], "r"))) { perror("file open"); return -1; } while (NULL != fgets(buff, BUFF_SIZE, fp)) { for (i = 0; i < strlen(buff) - 1; i++) { if (is_letter(buff[i]) && !is_letter(buff[i + 1])) cnt++; } } printf("words: %d \n", cnt); return 0; } int is_letter(char ch) { if ((ch >= ‘a‘ && ch <= ‘z‘) || (ch >= ‘A‘ && ch <= ‘Z‘)) return 1; else return 0; }
求一个文本文件中有多少个单词的方法,布布扣,bubuko.com
原文:http://blog.csdn.net/createchance/article/details/23687445