C語言每日練習之統(tǒng)計文本單詞數(shù)及高頻詞
更新時間:2022年05月18日 10:58:23 作者:德宏大魔王
本文文大家準備了個C語言練習題:統(tǒng)計單詞數(shù)并找出頻率最高的單詞,文中的示例代碼講解詳細,對我們學習C語言有一定幫助,感興趣的可以了解一下
作業(yè)1:統(tǒng)計出txt文本里面的單詞數(shù),并找出頻率出現(xiàn)最高的單詞是哪個?
運行結果:
上代碼:
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 = ""; //初始化次數(shù) string words = "hihi hello,hihi,hello,hihi?"; var new_i = words.Split(new char[] { ' ', ',', '.', '?' },StringSplitOptions.RemoveEmptyEntries); Console.Write("總的單詞數(shù)量:{0}\n", new_i.Length); for (int i = 0; i < new_i.Length; i++) { //查詢每個單詞出現(xiàn)的次數(shù) var query = from key in new_i where key.ToUpperInvariant() == new_i[i].ToUpperInvariant() select key; int key_count = query.Count(); if (key_count > count) { count = key_count; tmp = new_i[i]; } } Console.Write("頻率出現(xiàn)最高的單詞是:{0}\n", tmp); Console.Write("次數(shù)為:{0}\n", count); Console.ReadKey(); } } }
基礎代碼運行成功!通過外部打開!
創(chuàng)建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 = ""; //初始化次數(shù) var new_i = file_A.Split(new char[] { ' ', ',', '.', '?' }, StringSplitOptions.RemoveEmptyEntries); Console.Write("總的單詞數(shù)量:{0}\n", new_i.Length); for (int i = 0; i < new_i.Length; i++) { //查詢每個單詞出現(xiàn)的次數(shù) var query = from key in new_i where key.ToUpperInvariant() == new_i[i].ToUpperInvariant() select key; int key_count = query.Count(); if (key_count > count) { count = key_count; tmp = new_i[i]; } } Console.Write("頻率出現(xiàn)最高的單詞是:{0}\n", tmp); Console.Write("次數(shù)為:{0}\n", count); Console.ReadKey(); } } }
運行截圖:
到此這篇關于C語言每日練習之統(tǒng)計文本單詞數(shù)及高頻詞的文章就介紹到這了,更多相關C語言統(tǒng)計文本單詞數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
OpenCV邊緣提取算法流程的實現(xiàn)(附DEMO)
本文主要介紹了OpenCV邊緣提取算法流程的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08