Letter frequency: Difference between revisions

Content added Content deleted
(Added Quackery.)
(Add Cowgol)
Line 1,438: Line 1,438:
Z:> 3
Z:> 3
</pre>
</pre>

=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
include "argv.coh";
include "file.coh";

# Get filename from command line
ArgvInit();
var file := ArgvNext();
if file == (0 as [uint8]) then
print("error: no file name\n");
ExitWithError();
end if;

# Open the file
var fcb: FCB;
if FCBOpenIn(&fcb, file) != 0 then
print("error: cannot open file\n");
ExitWithError();
end if;

# Counters for each letter
var letterCount: uint32[26];
MemZero(&letterCount as [uint8], @bytesof letterCount);

# Count every letter
var len := FCBExt(&fcb);
while len != 0 loop
len := len - 1;
var ch := (FCBGetChar(&fcb) | 32) - 'a';
if ch >= @sizeof letterCount then
continue;
end if;
letterCount[ch] := letterCount[ch] + 1;
end loop;

# Close the file
var foo := FCBClose(&fcb);

# Print value for each letter
ch := 0;
while ch < @sizeof letterCount loop
print_char(ch + 'A');
print(": ");
print_i32(letterCount[ch]);
print_nl();
ch := ch + 1;
end loop;</lang>

{{out}}

The result of running the program on its own source file:

<pre>A: 22
B: 11
C: 46
D: 9
E: 80
F: 32
G: 6
H: 26
I: 42
J: 0
K: 0
L: 39
M: 7
N: 53
O: 45
P: 14
Q: 0
R: 47
S: 8
T: 59
U: 18
V: 11
W: 5
X: 4
Y: 2
Z: 3</pre>


=={{header|D}}==
=={{header|D}}==