The point is to calculate the index of coincidence when two strings of equal length are entered. For example, with
hellotherehowareyou
heycanhalstaytotalk
there are 19 words and positions 1 (h) and 7 (h) contain the same letter so the index of coincidence is 10.5%
import java.util.*;
public class Index
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("input a string of characters: ");
String firstLine = new String (input.nextLine());
System.out.println();
System.out.println("input a string of characters of equal length: ");
String secondLine = new String (input.nextLine());
String indexOfCoincidence = new String ( indexOfCoincidence(String firstLine, String secondLine));
System.out.println("The index of Coincidence is " + String indexOfCoincidence);
}
public float indexOfCoincidence(String X, String Y)
{
int sameChars = 0;
for( int i = 0; i < totalLength; ++i )
{
if( X.charAt(i) == Y.charAt(i) )
++sameChars;
}
float ioc = (float)sameChars / (float)totalLength;
return ioc;
}
}

New Topic/Question
Reply



MultiQuote




|