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

