c#英文單詞分類統(tǒng)計示例分享
更新時間:2014年03月04日 09:42:34 投稿:zxhpj
本文給出的題目是給出一段英文,對其分類統(tǒng)計出英文單詞的個數(shù)如:長度為4的單詞有2個,長度為3的有1個,下面是題目答案
復(fù)制代碼 代碼如下:
using System;
using System.Linq;
namespace ConsoleApplication1
{
/// <summary>
/// 給出一段英文,分類統(tǒng)計(如:長度為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();
}
}
}
相關(guān)文章
C#實現(xiàn)JWT無狀態(tài)驗證的實戰(zhàn)應(yīng)用解析
這篇文章主要介紹了C#實現(xiàn)JWT無狀態(tài)驗證的實戰(zhàn)應(yīng)用解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
利用C#實現(xiàn)獲取與監(jiān)控電腦系統(tǒng)信息
在C#中,獲取與監(jiān)控電腦系統(tǒng)信息通常可以通過多種方式實現(xiàn),這篇文章主要為大家整理了幾種常見的方法及其示例代碼,希望對大家有所幫助2024-11-11

