Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

9,848 questions

13,460 answers

573 users

How to count the occurrence of each character in a string with C#

1 Answer

0 votes
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string s = "c# programming language";
        
        var occurrences = s.Where(char.IsLetterOrDigit).ToLookup(ch => ch); 

        foreach (var chars in occurrences) {
            Console.Write(chars.Key + ": ");
            
            int i = 0;
            foreach (var ch in chars) {
                Console.Write("{0} ", ch);
                i++;
            }
            Console.WriteLine("({0})", i);
        }
    }
}


/*
run:

c: c (1)
p: p (1)
r: r r (2)
o: o (1)
g: g g g g (4)
a: a a a (3)
m: m m (2)
i: i (1)
n: n n (2)
l: l (1)
u: u (1)
e: e (1)

*/

 




answered Feb 7 by avibootz
...