Display count of characters enter by user in Console Application using C# and VB.Net

erkin.headshot
 
on Apr 22, 2022 10:57 PM
233 Views

I like to create a small program that print the number of each character and print it in big to small order.

Let me for explain

need to get int , char, string or anything from user i mean with Console.Readline();

Example : User enter 'excellent'

output be like 

e- 3 time

l - 2 time 

x - 1 time

c - 1 time 

n - 1 time

t - 1 time

or

Example : User enter '1233344'

output be like 

3 - 3 time

4- 2 time

1 - 1 time

2- 1 time

ty for helping

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Apr 22, 2022 11:58 PM

Hi erkin.headsho...,

Please refer below sample.

Code

C#

static void Main(string[] args)
{
    Console.WriteLine("Please enter value");
    var values = Console.ReadLine();
    char[] uniquValues = values.ToCharArray().Distinct().OrderBy(x => x).ToArray();
    foreach (char item in uniquValues)
    {
        int count = values.ToCharArray().Where(x => x == item).Count();
        Console.WriteLine(string.Format("{0} - {1} times.", item, count));
    }
    Console.Read();
}

VB.Net

Private Sub Main(ByVal args As String())
    Console.WriteLine("Please enter value")
    Dim values = Console.ReadLine()
    Dim uniquValues As Char() = values.ToCharArray().Distinct().OrderBy(Function(x) x).ToArray()

    For Each item As Char In uniquValues
        Dim count As Integer = values.ToCharArray().Where(Function(x) x = item).Count()
        Console.WriteLine(String.Format("{0} - {1} times.", item, count))
    Next

    Console.Read()
End Sub

Screenshot