Program to find how many times a character repeated in a given string using VB.NET

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 2047
Suppose we have a string and we need to figure out how many characters have been repeated.The below code will help to do so

Imports System.Linq
Namespace ConsoleApplication
Class Program
Private Shared Sub Main(args As String())
Dim lstWords As String = "hello this is dotnet funda"
(From g In From character In lstWords.ToCharArray().Where(Function(x) Not [Char].IsWhiteSpace(x))Group character By characterNew With { _
Key .Characters = g.Key, _
Key .RepeatedCharacters = If((g.Count() - 1) = 0, [String].Concat("Character ", g.Key, " has never repeated"), [String].Concat("Character ", g.Key, " repeated ", g.Count(), " times")) _
}).ToList().ForEach(Function(i) Console.WriteLine("{0} : {1}", i.Characters, i.RepeatedCharacters))
Console.ReadKey(True)
End Sub
End Class
End Namespace

Observe that we are removing the WhiteSpaces.Final output

h : Character h repeated 2 times
e : Character e repeated 2 times
l : Character l repeated 2 times
o : Character o repeated 2 times
t : Character t repeated 3 times
i : Character i repeated 2 times
s : Character s repeated 2 times
d : Character d repeated 2 times
n : Character n repeated 2 times
f : Character f has never repeated
u : Character u has never repeated
a : Character a has never repeated

Hope this will be helpful.

Comments or Responses

Login to post response