Finding the IC without doing the counting every single time
by fortenforge, Nov 14, 2009, 2:45 AM
Finding the IC of a plaintext can be incredibly tedious, especially if the the text is long. There are some good online sites that will calculate the IC for you, here is a good one that not only will calculate the IC, but will also give you other statistics and allow you to manipulate the text in some ways: http://rumkin.com/tools/cipher/manipulate.php.
You can also write your own program to calculate the IC.
Here is a java program I wrote that will do the job.
You can also write your own program to calculate the IC.
Here is a java program I wrote that will do the job.
/*
This program measures the index of conincidence on a given text.
Date: First draft 11/2/09
*/
import java.io.*;
import java.util.*;
public class indexOfCoincidence {
public static void main(String [] args) throws IOException {
long start = System.currentTimeMillis(); // start timing
BufferedReader f = new BufferedReader(new FileReader("indexOfCoincidence.in"));
String plaintext = f.readLine();
int[] charfrequency = new int[26];
int i = 0;
int j = 0;
int textlength = 0;
while(i < plaintext.length()) {
char c = plaintext.charAt(i);
j = (int) c - (int) 'A';
if(j >= 0 && j <= 25) {
charfrequency[j]++;
textlength++;
}
i++;
j = 0;
}
double ic = 0;
for(int k = 0; k < 26; k++)
ic += charfrequency[k] * (charfrequency[k] - 1);
ic /= textlength * (textlength - 1);
System.out.println("The index of coincidence is " + ic + ".");
}
}