Home > Programming > Letter Frequency Counter Using LINQ

Letter Frequency Counter Using LINQ

A year ago I was taking an Information Security class and we learned about how to decrypt messages that were encrypted using Substitution ciphers. The trick was to first know what language was it, the percentage frequency of letters in that language, and the frequency of the characters in the given message. As a homework assignment, we had to decrypt a message that was encrypted using a substitution cipher. Instead of looking up online, I jabbed my keyboard and wrote a Java program to count the frequency for me. Later, I came to realize sites dedicated just to do that. Apart from the above story, I was messing with the LINQPad 4 and trying to learn some LINQ. Here is how to count the frequency of characters in a string using LINQ:

var toks =
 from t in "The red green blue bird flew over the zoned-out programmer's 10 feet long desk."
 group t by char.ToLower(t) into g
 orderby g.Key
 select new { g.Key, Count = g.Count() };
 toks.Dump(); // note this a LINQPad syntax only

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: