iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C语言每日练习之统计文本单词数及高频词
  • 806
分享到

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

2024-04-02 19:04:59 806人浏览 独家记忆
摘要

作业1:统计出txt文本里面的单词数,并找出频率出现最高的单词是哪个? 运行结果: 上代码: using System; using System.Collections.Gene

作业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 < 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

通过外部打开:

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();
            
        }
    }
}

运行截图:

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

--结束END--

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

本文链接: https://www.lsjlt.com/news/149068.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作