public class CharDistribution
extends java.lang.Object
Analyzing character counts is a straightforward way to break any simple substitution cipher. Given a large enough corpus, text in natural languages like English have extremely similar distributions of characters. You can convince yourself of that using the main method below, which compares Rent and Hamlet.
By computing the distribution of the characters in a ciphertext, a crytographer can begin the process of breaking simple encryption schemes. (Letter frequency also has something to do with the per-letter scores in Scrabble.)
The provided code is incomplete. Modify it so that it works properly and passes the unit tests in
CharacterDistributionTest.java
.
Constructor and Description |
---|
CharDistribution() |
Modifier and Type | Method and Description |
---|---|
static double[] |
computeDistribution(java.lang.String corpus)
Return the distribution of characters in a string.
|
static void |
main(java.lang.String[] unused)
Compare the character distributions in Rent.txt and Hamlet.txt.
|
public static double[] computeDistribution(java.lang.String corpus)
Returns an array of doubles such that array[0] through array[25] are the fractions of characters for 'a-z' and array[26] through array[51] are the fractions for 'A-Z'. Other characters are not included in the array or the count.
corpus
- the corpuspublic static void main(java.lang.String[] unused)
You are free to review this function, but should not modify it. Note that this function is not tested by the test suite, as it is purely to aid your own interactive testing.
unused
- unused input arguments