If two strings are superimposed on one another, then some letters may match one
ID: 3638927 • Letter: I
Question
If two strings are superimposed on one another, then some letters may match one another. For
example, in the two strings
wonderwhowrotethebookonlove
weallliveinayellowsubmarine //letter "w" "e" "e" is marked in blue
there are three positions that contain the same letter: the first (w), the fourteenth (e), and the twentyseventh
(e). Of 27 possible positions, matches occur in three of them, or 11.1%. This percentage is
called the index of coincidence for two strings. It is more than just a curiosity; during World War II, it
was used to help decrypt enemy ciphers.
Complete the coincidence() method. This method takes two Strings of equal length as its
arguments. The method computes and returns its arguments’ index of coincidence as a double value.
For normal written English, the index of coincidence has an average value of 6.6%; for random strings
of letters, it is much lower, around 3.8% on average.
You may assume that the String arguments supplied to your method are of equal length, and consist
solely of lowercase letters (no spaces, digits, punctuation, or capital letters). Use a loop to examine the
pairs of corresponding characters (i.e., the characters that appear at the same relative position in their
Strings).
public static double coincidence (String first, String second)
{
// Fill in this method for the homework
return -1; // CHANGE THIS LINE
}
Explanation / Answer
Hi, here's a working implementation of your coincidence method with plenty of comments explaining each step included in the code. That should help you break it down and understand each part of it as you read it. I also went ahead and put it in a test class with a main execution method that uses those 2 strings you present as an example and prints out the outcome using the coincidence method. I highlighted what you need from the class for your homework in yellow, the rest is just extra to put it in context and test it a bit to get a better understanding of how it should be done. So don't copy-paste and move on :) study it properly so you know what to do next time. If something's missing or not clear, then just PM me and we can look at it again.
I hope this helps, please remember to rate!
public class IndexOfCoincidenceTest {
public static double coincidence(String first, String second) {
// variable to store index of coincidence
double indexOfCoincidence = 0;
// variable to track number of matches between both strings
int matchCount = 0;
// loop to go through the strings and match characters
// we assume first and second strings have same length
for(int i = 0; i < first.length(); i++)
{
// use String charAt(x) method to get charater at index x
// from a string and compare them
if(first.charAt(i) == second.charAt(i))
{
// if the characters match - increment the matchCount
matchCount++;
}
} // end of loop
// compute index of coincidence using matchCount / 27
// if matchCount is 0 - then hte index will be 0% as well
indexOfCoincidence = (double) matchCount / 27;
// multiply by 100 for correct percent % form
indexOfCoincidence = indexOfCoincidence * 100;
// return the indexOfCoincidence
return indexOfCoincidence;
} // end of coincidence method
// start of main execution method
public static void main(String[] args) {
// Test the coincindence function with 2 strings
String first = "wonderwhowrotethebookonlove";
String second = "weallliveinayellowsubmarine";
// decimal format to control how much
// precision to print out for doubles
java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
System.out.println("Index of coincidence between: " + first
+ " and " + second + " is : ");
double index = coincidence(first, second);
System.out.println( df.format(index) + " %");
}// end of main method
}// end of IndexOfCoincidenceTest class