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

Rajnilari2015
Posted by Rajnilari2015 under LINQ category on | Points: 40 | Views : 838
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.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string lstWords = "hello this is dotnet funda . dotnet funda is a dotnet forum";
(from word in lstWords.Split(' ')
group word by word into g
select 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);
}
}
}


Hope this will be helpful.

Comments or Responses

Login to post response