Need help with index of coincedence

• 10-30-2011, 05:20 PM
tripline
Need help with index of coincedence
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%.

Is there a website I can view to learn to do this? I tried searching but counldn't find anything.
• 10-30-2011, 05:36 PM
jMaster
Re: Need help with index of coincedence
I don't understand the problem here... Do you want to output 2 randomly generated strings of the same length?
• 10-30-2011, 05:43 PM
tripline
Re: Need help with index of coincedence
Question edited
• 10-30-2011, 05:57 PM
Fubarable
Re: Need help with index of coincedence
I'm confused: Why not simply try to write something like this yourself?
• 10-30-2011, 05:59 PM
tripline
Re: Need help with index of coincedence
Because the book did not have any instructions on how to do this. I just need to know how to check for matching letters.
• 10-30-2011, 06:02 PM
Fubarable
Re: Need help with index of coincedence
Quote:

Originally Posted by tripline
Because the book did not have any instructions on how to do this. I just need to know how to check for matching letters.

You can check if one char is the same as another with ==, and likely do this inside of nested for loops. The book isn't going to tell you exactly what to do in every programming situation. Instead it will give you general direction, and you're supposed to use your clever intellect to apply this information to help you solve this problem. In other words, please don't look to find a solution online, but instead put in some thought and figure it out on your own. You'll be glad you did.
• 10-30-2011, 06:12 PM
jMaster
Re: Need help with index of coincedence
Oh. I'm sorry it was my fault I don't know about that.

What you are looking to learn is called String Handling.

try something like this:

private void findIndexOfCoincidence(String word1, String word2) {
int count = 0;
if(word1.length() == word2.length()) {
for(int i = 0; i < word1.length(); i++){
if(word1.charAt(i) == word2.charAt(i)){
count++;
}
}
int index = (count/word1.length()) * 100;
}
}

I hope this is right theres a whole load of easels out now hungry for my laptop light and i wrote that in this post. Hope its what you're looking for..
• 10-30-2011, 06:13 PM
jMaster
Re: Need help with index of coincedence
Sorry that the indenting all got cut off i didn't do that
• 10-30-2011, 06:18 PM
Fubarable
Re: Need help with index of coincedence
jMaster: better to answer specific questions than to spoon-feed ready-made solutions. My experience here has taught me that doing this only teaches the student to beg and not to learn (though he will protest to the contrary, just wait). Also, a cursory glance at your solution suggests it may run into int division issues.
• 10-30-2011, 06:21 PM
doWhile
Re: Need help with index of coincedence
• 10-30-2011, 06:23 PM
Fubarable
Re: Need help with index of coincedence
Quote:

Originally Posted by doWhile

Thanks for the heads up. Original poster, please read copeg's links as they apply to this and all other programming fora that I know of.
• 10-30-2011, 06:24 PM
jMaster
Re: Need help with index of coincedence
Yes ofcourse index should be a double and either word1.length or count.

Okay i'm new to this forum things because my teacher said i'm already so good I need forums to make me know everything. I guess spoon-feeding is a bit of a waste of time also huh, ok i'll listen and not spoon-feed solutions.
• 10-30-2011, 06:24 PM
tripline
Re: Need help with index of coincedence
I am completely new at this. I have only been using public (...) so I don't know much about private. But thanks, ill try to figure your method out.

well, Im not completely blank. I meant it when I said i only needed help with matching letters.
I think either check each char one at a time (don't know how to write that)
or change string to array then check index one at a time.

import java.util.*;
public class indexofcoincedence
{public static void main (String[] args)
{
int len1=0; int len2=0;
char[] string1; char[] string2;
Scanner input = new Scanner(System.in);
System.out.println( "Enter a word");
String word1 = input.nextLine();
System.out.println( "Enter a word");
String word2 = input.nextLine();
len1=word1.length();
len2=word2.length();
string1=word1.toCharArray();
string2=word2.toCharArray();
}
}

This is as far as i got.