Program to find how many times a word repeated in a given string using LAMBDA and C#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1264
Suppose we have a string of words and we need to figure out how many of them have been repeated.The below code will help to do so

using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
List<string> lstWords = new List<string>() { "hello","this","is","dotnet","dotnet","funda","is","is"};
lstWords .GroupBy(x => x) .Select(g => new { Words = g.Key, WordsRepeated = (g.Count() - 1) == 0

? String.Concat("Word ", g.Key, " has never repeated")
: String.Concat("Word ", g.Key, " repeated ", g.Count(), " times")
}).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated));
Console.ReadKey(true);
}
}
}

Comments or Responses

Posted by: Sandep738 on: 1/1/2016 Level:Starter | Status: [Member] | Points: 10
Thank you!!
Posted by: Rajnilari2015 on: 1/1/2016 Level:Platinum | Status: [Member] [Microsoft_MVP] [MVP] | Points: 10
@Sandep738, glad u liked

Login to post response