首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【C#每日一题】统计单词数并找出频率最高的单词

【C#每日一题】统计单词数并找出频率最高的单词

作者头像
园游会永不打烊
发布2023-08-08 14:03:34
发布2023-08-08 14:03:34
5840
举报
文章被收录于专栏:cloud stdiocloud stdio

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

运行结果:

上代码:

代码语言:javascript
复制
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 < new_i.Length; i++)
            {
                //查询每个单词出现的次数
                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("频率出现最高的单词是:{0}\n", tmp);
            Console.Write("次数为:{0}\n", count);
            
            Console.ReadKey();
            
        }
    }
}

========================

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

创建11.txt

通过外部打开:

代码语言:javascript
复制
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 < new_i.Length; i++)
            {
                //查询每个单词出现的次数
                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("频率出现最高的单词是:{0}\n", tmp);
            Console.Write("次数为:{0}\n", count);
            
            Console.ReadKey();
            
        }
    }
}

运行截图:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档