Skip to content

Using arrays

NN--- edited this page Apr 18, 2012 · 4 revisions

Using arrays

  • Category: Arrays, Hash Tables and Dictionaries
  • Description: Create a histogram of the occurrences of particular letters
  • Code:
using System.Console;

def results    = array('Z' + 1 - 'A') : array[int];
def data       = "The quick brown fox jumps over the lazy dog";

foreach (c when c >= 'A' && c <= 'Z' in data.ToUpper() with i)
  results[c : int - 'A']++;
    
foreach (c in ['A' .. 'Z']) 
  WriteLine($"Number of '$c' characters = $(results[c - 'A' : int])");

Execution Result:

Number of 'A' characters = 1
Number of 'B' characters = 1
Number of 'C' characters = 1
Number of 'D' characters = 1
Number of 'E' characters = 3
Number of 'F' characters = 1
Number of 'G' characters = 1
Number of 'H' characters = 2
Number of 'I' characters = 1
Number of 'J' characters = 1
Number of 'K' characters = 1
Number of 'L' characters = 1
Number of 'M' characters = 1
Number of 'N' characters = 1
Number of 'O' characters = 4
Number of 'P' characters = 1
Number of 'Q' characters = 1
Number of 'R' characters = 2
Number of 'S' characters = 1
Number of 'T' characters = 2
Number of 'U' characters = 2
Number of 'V' characters = 1
Number of 'W' characters = 1
Number of 'X' characters = 1
Number of 'Y' characters = 1
Number of 'Z' characters = 1

[Copyright ©](Terms of use, legal notice)

Clone this wiki locally