Program to find how many times a word repeated in a given string using LAMBDA and VB.NET

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 409
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

Imports System.Collections.Generic
Imports System.Linq
Namespace ConsoleApplication
Class Program
Private Shared Sub Main(args As String())
Dim lstWords As New List(Of String)() From { _
"hello", _
"this", _
"is", _
"dotnet", _
"dotnet", _
"funda", _
"is", _
"is" _
}

lstWords.GroupBy(Function(x) x).[Select](Function(g) New With { _
Key .Words = g.Key, _
Key .WordsRepeated = If((g.Count() - 1) = 0, [String].Concat("Word ", g.Key, " has never repeated"), [String].Concat("Word ", g.Key, " repeated ", g.Count(), " times")) _
}).ToList().ForEach(Function(i) Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated))
Console.ReadKey(True)
End Sub
End Class
End Namespace

Comments or Responses

Login to post response