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

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 199
using System;

using System.Collections.Generic;

using System.Linq;



namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

//Question #4: Find how many times a word repeated in a given string.

List<string> lstWords = new List<string>() { "hello","this","is","dotnet","dotnet","funda","is","is"};



(from word in lstWords

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);

}

}

}


Output
----------

hello : Word hello has never repeated

this : Word this has never repeated

is : Word is repeated 3 times

dotnet : Word dotnet repeated 2 times

funda : Word funda has never repeated

Comments or Responses

Login to post response