C語言每日練習(xí)之統(tǒng)計(jì)文本單詞數(shù)及高頻詞
作業(yè)1:統(tǒng)計(jì)出txt文本里面的單詞數(shù),并找出頻率出現(xiàn)最高的單詞是哪個(gè)?
運(yù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++)
{
//查詢每個(gè)單詞出現(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ǔ)代碼運(yùn)行成功!通過外部打開!

創(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++)
{
//查詢每個(gè)單詞出現(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();
}
}
}運(yùn)行截圖:

到此這篇關(guān)于C語言每日練習(xí)之統(tǒng)計(jì)文本單詞數(shù)及高頻詞的文章就介紹到這了,更多相關(guān)C語言統(tǒng)計(jì)文本單詞數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Cocos2d-x學(xué)習(xí)筆記之Hello World源碼分析
這篇文章主要介紹了Cocos2d-x學(xué)習(xí)筆記之Hello World源碼分析,接上一篇內(nèi)容,本文著重分析源碼文件,需要的朋友可以參考下2014-09-09
atoi和itoa函數(shù)的實(shí)現(xiàn)方法
vs2022?qt環(huán)境搭建調(diào)試的方法步驟
理解C++編程中的std::function函數(shù)封裝
使用opencv實(shí)現(xiàn)車道線檢測實(shí)戰(zhàn)代碼
C++的template模板中class與typename關(guān)鍵字的區(qū)別分析
C語言中實(shí)現(xiàn)KMP算法的實(shí)例講解

