Question

In Java Index of Coincidence If two strings of equal length are superimposed on one another,...

In Java

Index of Coincidence

If two strings of equal length are superimposed on one another, then some letters may match. For example consider the strings

wonderwhowrotethebookonlove and weallliveinayellowsubmarine

Notice that there are three positions that contain the same letter: the 1 st (w) , 14 th (e), and 27 th (e). Of 27 possible positions, matches occur in three positions (11.1%). This percentage is called the index of coincidence for two strings, and it is used to decrypt ciphers like those used by the Germans in World War II.

Write a program that accepts two strings of the same length and determines their index of coincidence. For normal written English, the index of coincidence averages about 6.6%, while for random strings it is 1/26, or around 3.8%.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class IndexOfCoincidence extends JPanel{

   public static void main(String[] args) {

       System.out.println("IofC "+indexOfCoincidence("wonderwhowrotethebookonlove","weallliveinayellowsubmarine"));
   }
  
   public static double indexOfCoincidence(String str1, String str2){
       double iOfC =0.0;
       if(str1.length() != str2.length()){
           System.out.println("Strings must be of same length");
           return iOfC;
       }
       int count=0;
       for(int i=0; i<str1.length(); i++){
           if(str1.charAt(i)==str2.charAt(i))
               count++;
       }
       iOfC = ((double)count/(double)str1.length())*100.0d;
       return iOfC;
   }

}

Add a comment
Know the answer?
Add Answer to:
In Java Index of Coincidence If two strings of equal length are superimposed on one another,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT