C語言每日練習(xí)之統(tǒng)計文本單詞數(shù)及高頻詞
作業(yè)1:統(tǒng)計出txt文本里面的單詞數(shù),并找出頻率出現(xiàn)最高的單詞是哪個?
運行結(jié)果:
上代碼:
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(); } } }
基礎(chǔ)代碼運行成功!通過外部打開!
創(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(); } } }
運行截圖:
到此這篇關(guān)于C語言每日練習(xí)之統(tǒng)計文本單詞數(shù)及高頻詞的文章就介紹到這了,更多相關(guān)C語言統(tǒng)計文本單詞數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenCV邊緣提取算法流程的實現(xiàn)(附DEMO)
本文主要介紹了OpenCV邊緣提取算法流程的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08C++解決大數(shù)組棧內(nèi)存不夠問題的方法分析
這篇文章主要介紹了C++解決大數(shù)組棧內(nèi)存不夠問題的方法,結(jié)合實例形式對比分析了C++針對大數(shù)組棧內(nèi)存不足情況的常見解決方法及其優(yōu)缺點,具有一定參考借鑒價值,需要的朋友可以參考下2018-05-05