|
|
Character statistics
/csbook/solutions/18/A10.cs
using System;
using System.Collections;
using System.IO;
// Reads a text file and computes the frequencies of
// ASCII characters in it.
class CharacterStat {
static void Main(string[] arg) {
BitArray used = new BitArray(128);
if (arg.Length > 0) {
//--- read text and count characters
string fileName = arg[0];
StreamReader r = File.OpenText(fileName);
int ch = r.Read();
while (ch >= 0) {
if (0 <= ch && ch < 128) used[ch] = true;
ch = r.Read();
}
r.Close();
//--- output character frequencies
Console.WriteLine("The following ASCII characters occur in {0}:", fileName);
for (int i = ' '; i < 127; i++)
if (used[i]) Console.Write((char)i + " ");
} else {
Console.WriteLine("-- no input file specified");
}
}
}
|
|