How to print histogram of frequencies of characters in string with C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        int[] lettercount = new int[256];
        string s = "C# is a general-purpose, multi-paradigm programming language.";
     
        for (int i = 0; i < s.Length; i++) {
            lettercount[s[i]] += 1;
        }
 
        for (int i = 0; i < 256; i++) {
            if (lettercount[i] != 0) {
                String stars = "";
                for(int j = 0; j < lettercount[i]; j++){
                    stars += "*";
                }
                Console.WriteLine((char)i + " " + stars );
            }
        }
    }
}




/*
run:
 
  ******
# *
, *
- **
. *
C *
a *******
d *
e ****
g ******
i ****
l ***
m ****
n ***
o **
p ****
r *****
s **
t *
u ***
 
*/

 



answered Oct 4, 2021 by avibootz
...