java在一段密文中计算出bigram
试图找出为什么我的bi-gram计数器在数组中没有正确排序,并保持正确的匹配计数。非常感谢您的帮助
例如-->;“DLKJGJFFDLJDDLJLDLFK” 答案-->;DL=4
int cipherTxt_length = cipherTxt.length();
String ch;
int count=0;
for(char i='a'; i<='z'; i++)
{
System.out.println("inside first for loop -- count -- "+count);
for(char k='a'; k<='z'; k++)
{
System.out.println("inside first for loop -- count -- "+count);
count = 0;
for(int j=0; j<cipherTxt_length; j++)
{
System.out.println("inside first for loop -- count -- "+count);
ch=cipherTxt.substring(j,j+2); //extracting characters of the string one by one
if(ch.charAt(j)==k && ch.charAt(j+2)==i) //first checking the whole string for 'a', then 'b' and so on
count++; //increasing count of those aplhabets which are present in the string
}
if(count!=0)//printing only those aphabets whose count is not '0'
{
System.out.println(i+"\t\t"+count);//\t is tabbing method
}
}
}
}
# 1 楼答案
试试这个