C语言每日练习之统计文本单词数及高频词

本文文大家准备了个C语言练习题:统计单词数并找出频率最高的单词,文中的示例代码讲解详细,对我们学习C语言有一定帮助,感兴趣的可以了解一下

作业1:统计出txt文本里面的单词数,并找出频率出现最高的单词是哪个?

运行结果:

上代码:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //文件打开 //string file = System.IO.File.ReadAllLines(@""); int count = 0; string tmp = ""; //初始化次数 string words = "hihi hello,hihi,hello,hihi?"; var new_i = words.Split(new char[] { ' ', ',', '.', '?' },StringSplitOptions.RemoveEmptyEntries); Console.Write("总的单词数量:{0}\n", new_i.Length); for (int i = 0; i  count) { count = key_count; tmp = new_i[i]; } } Console.Write("频率出现最高的单词是:{0}\n", tmp); Console.Write("次数为:{0}\n", count); Console.ReadKey(); } } } 

基础代码运行成功!通过外部打开!

创建11.txt

通过外部打开:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //文件打开 //string[] file_A = System.IO.File.ReadAllLines(@"C:\Users\Administrator\Desktop\11.txt"); string file_A = System.IO.File.ReadAllText(@"C:\Users\Administrator\Desktop\11.txt"); //Console.Write(file_A); int count = 0; string tmp = ""; //初始化次数 var new_i = file_A.Split(new char[] { ' ', ',', '.', '?' }, StringSplitOptions.RemoveEmptyEntries); Console.Write("总的单词数量:{0}\n", new_i.Length); for (int i = 0; i  count) { count = key_count; tmp = new_i[i]; } } Console.Write("频率出现最高的单词是:{0}\n", tmp); Console.Write("次数为:{0}\n", count); Console.ReadKey(); } } }

运行截图:

到此这篇关于C语言每日练习之统计文本单词数及高频词的文章就介绍到这了,更多相关C语言统计文本单词数内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是C语言每日练习之统计文本单词数及高频词的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言