TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Count Letter Frequencies

This C# article implements a program that counts the frequencies of letters in text.

Letter frequencies. English text contains some letters more than others.

It is possible to compute a table of letter frequencies. We do this with a method in the C# language. This may be part of a homework assignment or required for simple textual analysis.

Example. The first part of the solution is declaring an array to store the frequencies. In the C# programming language, the char type is 2 bytes, which means it contains 65535 elements to store.

Int Array

C# program that counts frequency of letters

using System;
using System.IO;

class Program
{
    static void Main()
    {
	// 1.
	// Array to store frequencies.
	int[] c = new int[(int)char.MaxValue];

	// 2.
	// Read entire text file.
	string s = File.ReadAllText("text.txt");

	// 3.
	// Iterate over each character.
	foreach (char t in s)
	{
	    // Increment table.
	    c[(int)t]++;
	}

	// 4.
	// Write all letters found.
	for (int i = 0; i < (int)char.MaxValue; i++)
	{
	    if (c[i] > 0 &&
		char.IsLetterOrDigit((char)i))
	    {
		Console.WriteLine("Letter: {0}  Frequency: {1}",
		    (char)i,
		    c[i]);
	    }
	}
    }
}

Output

(Output is shown in "Tests" section.)

Description of part 1. To declare the array, we will use the new char syntax and initialize the array size to char.MaxValue, which is a constant value defined in the .NET Framework.

Char

Description of part 2. Next, we read in the file where we are counting letters. The File.ReadAllText method efficiently reads the file and places its contents into a string. We need no loop for this operation.

File.ReadAllText

Next: We add up frequencies by iterating over each letter, and casting it to an int. We increment each "slot" for the letters we find.

Finally: We display all letter or digit characters we found. The Console.WriteLine statement actually prints the results to the screen.

Console.WriteLine

Correct. Incorrect code is worse than useless in production environments. The following table shows the input file I used and the output from the program. We see that the frequencies are correct.

Input file

aaaa
bbbbb
aaaa
bbbbb
aaaa bbbbb
CCcc
xx
y y y y y
Z

Output

Letter: C  Frequency: 2
Letter: Z  Frequency: 1
Letter: a  Frequency: 12
Letter: b  Frequency: 15
Letter: c  Frequency: 2
Letter: x  Frequency: 2
Letter: y  Frequency: 5

Summary. We counted the letter frequencies in a string or file using the C# language. This is handy code to have for text-based processing. The code I showed works correctly on the input files I tested, one of which I documented above.

Review: Most important in the solution is how the array is declared and the constants are used.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf