c#英文单词分类统计示例分享

本文给出的题目是给出一段英文,对其分类统计出英文单词的个数如:长度为4的单词有2个,长度为3的有1个,下面是题目答案

复制代码 代码如下:

using System;
using System.Linq;
namespace ConsoleApplication1
{
    ///
    /// 给出一段英文,分类统计(如:长度为4的单词有2个:time,well)
    ///

    class Program
    {
        static void Main(string[] args)
        {
            string source = "Do one thing at a time,and do well";//已知英文语句
            string[] stringArray = source.Split(new char[] { ' ', ',' });
            var result = stringArray.GroupBy(s => s.Length).Select(s => new {
                Lenght = s.Select(x => x).FirstOrDefault().Length,
                Count = s.Count(),
                StringItems = s.Select(x => x)
            });

            foreach (var s in result)
            {
                string strResult = string.Empty;
                foreach (var item in s.StringItems)
                {
                    strResult += string.IsNullOrEmpty(strResult) ? item : " , " + item;
                }
                Console.WriteLine(string.Format("长度为{0}的单词有{1}个:{2}", s.Lenght, s.Count, strResult));
            }           
            Console.ReadKey();
        }
    }
}

以上就是c#英文单词分类统计示例分享的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程