This project is read-only.
Using arrays

Category:
Arrays, Hash Tables and Dictionaries
Description: Create a histogram of the occurrences of particular letters
Code:
let SampleArray2() =
let numLetters = 26
let results = Array.create numLetters 0
let data = "The quick brown fox jumps over the lazy dog"
for i = 0 to data.Length - 1 do
let c = data.Chars(i)
let c = Char.ToUpper(c)
if c >= 'A' && c <= 'Z' then
let i = int c - int 'A'
results.[i] <- results.[i] + 1
for i = 0 to numLetters - 1 do
printfn "Number of '%c' characters = %d" (char (i + int 'A')) results.[i]

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

Last edited Sep 14, 2011 at 3:45 AM by ttliu2000, version 1

Comments

No comments yet.