Caesar Cipher using frequency analysis in Java
up vote
-1
down vote
favorite
Caesar Cipher using Frequency Analysis in Java.
This is my code for the decode part:
public static String decode (String code){
int key=0;
final int ALPHABET_SIZE = 26;
int freqs = new int[ALPHABET_SIZE];
for (int l=0;l<freqs.length;l++){
freqs[l]=0;
}
for (int k=0;k<code.length();k++){
if (code.charAt(k)>='a' && code.charAt(k)<='z'){
freqs[code.charAt(k)-'a']++;
}
}
int biggest = 0;
for (int t=0;t<freqs.length;t++){
if (freqs[t]>biggest){
biggest= t;
}
}
if (biggest<4){
key = (biggest + 26 - ('e'+'a'));
}
else{
key = biggest + 'a' - 'e';
}
return (decode(code,key));
}
I cannot use maps, imports, lists, or add a key, I do know that the most freq letter is E but I do not know how to implement it in a different function. I would appreciate a more elegant solution.
java caesar-cipher
add a comment |
up vote
-1
down vote
favorite
Caesar Cipher using Frequency Analysis in Java.
This is my code for the decode part:
public static String decode (String code){
int key=0;
final int ALPHABET_SIZE = 26;
int freqs = new int[ALPHABET_SIZE];
for (int l=0;l<freqs.length;l++){
freqs[l]=0;
}
for (int k=0;k<code.length();k++){
if (code.charAt(k)>='a' && code.charAt(k)<='z'){
freqs[code.charAt(k)-'a']++;
}
}
int biggest = 0;
for (int t=0;t<freqs.length;t++){
if (freqs[t]>biggest){
biggest= t;
}
}
if (biggest<4){
key = (biggest + 26 - ('e'+'a'));
}
else{
key = biggest + 'a' - 'e';
}
return (decode(code,key));
}
I cannot use maps, imports, lists, or add a key, I do know that the most freq letter is E but I do not know how to implement it in a different function. I would appreciate a more elegant solution.
java caesar-cipher
1
I am not sure this is actually doing what you want. In particular, I suspect that there is a bug where you allocate biggest.
– Josiah
2 days ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Caesar Cipher using Frequency Analysis in Java.
This is my code for the decode part:
public static String decode (String code){
int key=0;
final int ALPHABET_SIZE = 26;
int freqs = new int[ALPHABET_SIZE];
for (int l=0;l<freqs.length;l++){
freqs[l]=0;
}
for (int k=0;k<code.length();k++){
if (code.charAt(k)>='a' && code.charAt(k)<='z'){
freqs[code.charAt(k)-'a']++;
}
}
int biggest = 0;
for (int t=0;t<freqs.length;t++){
if (freqs[t]>biggest){
biggest= t;
}
}
if (biggest<4){
key = (biggest + 26 - ('e'+'a'));
}
else{
key = biggest + 'a' - 'e';
}
return (decode(code,key));
}
I cannot use maps, imports, lists, or add a key, I do know that the most freq letter is E but I do not know how to implement it in a different function. I would appreciate a more elegant solution.
java caesar-cipher
Caesar Cipher using Frequency Analysis in Java.
This is my code for the decode part:
public static String decode (String code){
int key=0;
final int ALPHABET_SIZE = 26;
int freqs = new int[ALPHABET_SIZE];
for (int l=0;l<freqs.length;l++){
freqs[l]=0;
}
for (int k=0;k<code.length();k++){
if (code.charAt(k)>='a' && code.charAt(k)<='z'){
freqs[code.charAt(k)-'a']++;
}
}
int biggest = 0;
for (int t=0;t<freqs.length;t++){
if (freqs[t]>biggest){
biggest= t;
}
}
if (biggest<4){
key = (biggest + 26 - ('e'+'a'));
}
else{
key = biggest + 'a' - 'e';
}
return (decode(code,key));
}
I cannot use maps, imports, lists, or add a key, I do know that the most freq letter is E but I do not know how to implement it in a different function. I would appreciate a more elegant solution.
java caesar-cipher
java caesar-cipher
edited yesterday
200_success
127k15148411
127k15148411
asked 2 days ago
Yuki1112
92
92
1
I am not sure this is actually doing what you want. In particular, I suspect that there is a bug where you allocate biggest.
– Josiah
2 days ago
add a comment |
1
I am not sure this is actually doing what you want. In particular, I suspect that there is a bug where you allocate biggest.
– Josiah
2 days ago
1
1
I am not sure this is actually doing what you want. In particular, I suspect that there is a bug where you allocate biggest.
– Josiah
2 days ago
I am not sure this is actually doing what you want. In particular, I suspect that there is a bug where you allocate biggest.
– Josiah
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208233%2fcaesar-cipher-using-frequency-analysis-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I am not sure this is actually doing what you want. In particular, I suspect that there is a bug where you allocate biggest.
– Josiah
2 days ago