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 iThemes WordPress Hosting

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

DreamHost Web Hosting


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

Liquid Web Cloud VPS Hosting

10,935 questions

14,843 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
...