Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,955 questions

51,897 answers

573 users

How to count the frequency of the digits (0 to 9) in a string with C#

1 Answer

0 votes
using System;
 
public class Program
{
    private static void countDigits(string s) {
        int size = s.Length;
 
        if (size == 0) {
            Console.Write("String is empry");
            return;
        }
 
        int[] digit_frequency = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 
        for (int i = 0; i < size; i++) {
            if (char.IsDigit(s[i])) {
                digit_frequency[s[i] - '0']++;
            }
        }
 
        for (int j = 0; j < 10; j++) {
            Console.WriteLine(j + ": " + digit_frequency[j] + " times");
        }
    }
 
    public static void Main(string[] args)
    {
        string s = "c#23c++4523java23988rust82215";
 
        countDigits(s);
    }
}
 
 
 
 
 
 
/*
run:
 
0: 0 times
1: 1 times
2: 5 times
3: 3 times
4: 1 times
5: 2 times
6: 0 times
7: 0 times
8: 3 times
9: 1 times
 
*/

 



answered Jun 1, 2023 by avibootz
...