欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-20 11:59:04
复制代码 代码如下:using system;using system.linq;namespace consoleapplication1{  &nb...
复制代码 代码如下:

using system;
using system.linq;
namespace consoleapplication1
{
    /// <summary>
    /// 给出一段英文,分类统计(如:长度为4的单词有2个:time,well)
    /// </summary>
    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();
        }
    }
}

上一篇:

下一篇: