读书人

用C编写文本文件中的关键词查找与统计

发布时间: 2012-03-31 13:13:26 作者: rapoo

用C编写文本文件中的关键词查找与统计
Write a C program that reads in two words, then searches for all occurrences of these words within a text file. Your program must display the first line that each word occurs in and also display how many times the words occur in the file. Your program must not be case sensitive for example, the word “APPLE” must be recognised to be the same word as “apple”. You will need a file (or files) to test your program. You can use an existing file or make a new one. The file must be a text file. It cannot be a Word document or any file that requires a special format. Note that if the words do not occur in the file, you must display an appropriate message.

Example:
Enter the first word: the

Enter the second word: Bilbo

Enter the name of the text file: History.txt

The word ‘the’ first appears in line 3 which is:
The 8th Route Army infiltrated behind enemy lines in Northern China

The word ‘the’ appears 372 times in the file.

The word ‘Bilbo’ does not appear in the file.


[解决办法]

探讨
可以建立哈希表存储文本文件中的不同单词,
统计每个单词出现的频率,并记录某个第一次出现的位置,
然后按照关键词查找!

读书人网 >C语言

热点推荐