You could use a
Dictionary<char, int>
to count each character's occurrences.
Dictionary<char, int> occurrences = new Dictionary<char, int>();
string text = File.ReadAllText(filepath);
foreach (char c in text) {
if (!occurrences.ContainsKey(c)) {
occurrences.Add(c, 1);
}
else {
occurrences[c]++;
}
}
You will end with the number of occurrences of each character in the string. But it may get a little more difficult if your input text file contains unicode characters which cannot be represented by a simple
char
value.