Caesar Cipher Frequency Analysis in Java












0















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, thank you. ** Frequency Analysis










share|improve this question























  • A more elegant solution would use all the Java objects you have it's an oriented object language so a better solution would use it

    – azro
    Nov 22 '18 at 15:21











  • Letter frequencies: ETAOINSHRDLU... though [space] is more frequent than E.

    – rossum
    Nov 22 '18 at 16:23
















0















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, thank you. ** Frequency Analysis










share|improve this question























  • A more elegant solution would use all the Java objects you have it's an oriented object language so a better solution would use it

    – azro
    Nov 22 '18 at 15:21











  • Letter frequencies: ETAOINSHRDLU... though [space] is more frequent than E.

    – rossum
    Nov 22 '18 at 16:23














0












0








0








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, thank you. ** Frequency Analysis










share|improve this question














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, thank you. ** Frequency Analysis







java caesar-cipher frequency-analysis






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 15:10









Yuki1112Yuki1112

726




726













  • A more elegant solution would use all the Java objects you have it's an oriented object language so a better solution would use it

    – azro
    Nov 22 '18 at 15:21











  • Letter frequencies: ETAOINSHRDLU... though [space] is more frequent than E.

    – rossum
    Nov 22 '18 at 16:23



















  • A more elegant solution would use all the Java objects you have it's an oriented object language so a better solution would use it

    – azro
    Nov 22 '18 at 15:21











  • Letter frequencies: ETAOINSHRDLU... though [space] is more frequent than E.

    – rossum
    Nov 22 '18 at 16:23

















A more elegant solution would use all the Java objects you have it's an oriented object language so a better solution would use it

– azro
Nov 22 '18 at 15:21





A more elegant solution would use all the Java objects you have it's an oriented object language so a better solution would use it

– azro
Nov 22 '18 at 15:21













Letter frequencies: ETAOINSHRDLU... though [space] is more frequent than E.

– rossum
Nov 22 '18 at 16:23





Letter frequencies: ETAOINSHRDLU... though [space] is more frequent than E.

– rossum
Nov 22 '18 at 16:23












1 Answer
1






active

oldest

votes


















0














I am not too sure what your definition of "elegant" is, but your solution looks fine in general.



A few comments:




  1. You don't actually have to manually initialise your array to 0 at the start since Java will do that for you


  2. Looking for the letter with the highest frequency can be done while you are counting the characters



For example, the for loop can become:



int highestFreq = 0;
int highestFreqIdx = -1;
for (int k=0; k < code.length(); k++) {
if (code.charAt(k) >= 'a' && code.charAt(k) <= 'z') {
int count = freqs[code.charAt(k)-'a']++;

if (count > highestFreq) {
highestFreq = count;
highestFreqIdx = k;
}
}
}



  1. Calculating the key can be simplified


If I am not wrong, your key is the number of positions that the letter with the highest frequency is away from 'e'. In this case, you can simply do this:



key = biggest - ('e' - 'a');


So your code becomes:



public static String decode (String code){
int key = 0;
final int ALPHABET_SIZE = 26;
int freqs = new int[ALPHABET_SIZE];

int highestFreq = 0;
int highestFreqIdx = -1;
for (int k=0; k < code.length(); k++) {
if (code.charAt(k) >= 'a' && code.charAt(k) <= 'z') {
int count = freqs[code.charAt(k)-'a']++;

if (count > highestFreq) {
highestFreq = count;
highestFreqIdx = k;
}
}
}

key = highestFreqIdx - ('e' - 'a'); // Can also directly use 4 instead of 'e' - 'a'

return (decode(code, key));
}


That said, your code will only work for messages that really have the letter 'e' as the most common letter...



P/S Such code review questions may be better off in this Stack Exchange site for code reviews instead






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433812%2fcaesar-cipher-frequency-analysis-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I am not too sure what your definition of "elegant" is, but your solution looks fine in general.



    A few comments:




    1. You don't actually have to manually initialise your array to 0 at the start since Java will do that for you


    2. Looking for the letter with the highest frequency can be done while you are counting the characters



    For example, the for loop can become:



    int highestFreq = 0;
    int highestFreqIdx = -1;
    for (int k=0; k < code.length(); k++) {
    if (code.charAt(k) >= 'a' && code.charAt(k) <= 'z') {
    int count = freqs[code.charAt(k)-'a']++;

    if (count > highestFreq) {
    highestFreq = count;
    highestFreqIdx = k;
    }
    }
    }



    1. Calculating the key can be simplified


    If I am not wrong, your key is the number of positions that the letter with the highest frequency is away from 'e'. In this case, you can simply do this:



    key = biggest - ('e' - 'a');


    So your code becomes:



    public static String decode (String code){
    int key = 0;
    final int ALPHABET_SIZE = 26;
    int freqs = new int[ALPHABET_SIZE];

    int highestFreq = 0;
    int highestFreqIdx = -1;
    for (int k=0; k < code.length(); k++) {
    if (code.charAt(k) >= 'a' && code.charAt(k) <= 'z') {
    int count = freqs[code.charAt(k)-'a']++;

    if (count > highestFreq) {
    highestFreq = count;
    highestFreqIdx = k;
    }
    }
    }

    key = highestFreqIdx - ('e' - 'a'); // Can also directly use 4 instead of 'e' - 'a'

    return (decode(code, key));
    }


    That said, your code will only work for messages that really have the letter 'e' as the most common letter...



    P/S Such code review questions may be better off in this Stack Exchange site for code reviews instead






    share|improve this answer




























      0














      I am not too sure what your definition of "elegant" is, but your solution looks fine in general.



      A few comments:




      1. You don't actually have to manually initialise your array to 0 at the start since Java will do that for you


      2. Looking for the letter with the highest frequency can be done while you are counting the characters



      For example, the for loop can become:



      int highestFreq = 0;
      int highestFreqIdx = -1;
      for (int k=0; k < code.length(); k++) {
      if (code.charAt(k) >= 'a' && code.charAt(k) <= 'z') {
      int count = freqs[code.charAt(k)-'a']++;

      if (count > highestFreq) {
      highestFreq = count;
      highestFreqIdx = k;
      }
      }
      }



      1. Calculating the key can be simplified


      If I am not wrong, your key is the number of positions that the letter with the highest frequency is away from 'e'. In this case, you can simply do this:



      key = biggest - ('e' - 'a');


      So your code becomes:



      public static String decode (String code){
      int key = 0;
      final int ALPHABET_SIZE = 26;
      int freqs = new int[ALPHABET_SIZE];

      int highestFreq = 0;
      int highestFreqIdx = -1;
      for (int k=0; k < code.length(); k++) {
      if (code.charAt(k) >= 'a' && code.charAt(k) <= 'z') {
      int count = freqs[code.charAt(k)-'a']++;

      if (count > highestFreq) {
      highestFreq = count;
      highestFreqIdx = k;
      }
      }
      }

      key = highestFreqIdx - ('e' - 'a'); // Can also directly use 4 instead of 'e' - 'a'

      return (decode(code, key));
      }


      That said, your code will only work for messages that really have the letter 'e' as the most common letter...



      P/S Such code review questions may be better off in this Stack Exchange site for code reviews instead






      share|improve this answer


























        0












        0








        0







        I am not too sure what your definition of "elegant" is, but your solution looks fine in general.



        A few comments:




        1. You don't actually have to manually initialise your array to 0 at the start since Java will do that for you


        2. Looking for the letter with the highest frequency can be done while you are counting the characters



        For example, the for loop can become:



        int highestFreq = 0;
        int highestFreqIdx = -1;
        for (int k=0; k < code.length(); k++) {
        if (code.charAt(k) >= 'a' && code.charAt(k) <= 'z') {
        int count = freqs[code.charAt(k)-'a']++;

        if (count > highestFreq) {
        highestFreq = count;
        highestFreqIdx = k;
        }
        }
        }



        1. Calculating the key can be simplified


        If I am not wrong, your key is the number of positions that the letter with the highest frequency is away from 'e'. In this case, you can simply do this:



        key = biggest - ('e' - 'a');


        So your code becomes:



        public static String decode (String code){
        int key = 0;
        final int ALPHABET_SIZE = 26;
        int freqs = new int[ALPHABET_SIZE];

        int highestFreq = 0;
        int highestFreqIdx = -1;
        for (int k=0; k < code.length(); k++) {
        if (code.charAt(k) >= 'a' && code.charAt(k) <= 'z') {
        int count = freqs[code.charAt(k)-'a']++;

        if (count > highestFreq) {
        highestFreq = count;
        highestFreqIdx = k;
        }
        }
        }

        key = highestFreqIdx - ('e' - 'a'); // Can also directly use 4 instead of 'e' - 'a'

        return (decode(code, key));
        }


        That said, your code will only work for messages that really have the letter 'e' as the most common letter...



        P/S Such code review questions may be better off in this Stack Exchange site for code reviews instead






        share|improve this answer













        I am not too sure what your definition of "elegant" is, but your solution looks fine in general.



        A few comments:




        1. You don't actually have to manually initialise your array to 0 at the start since Java will do that for you


        2. Looking for the letter with the highest frequency can be done while you are counting the characters



        For example, the for loop can become:



        int highestFreq = 0;
        int highestFreqIdx = -1;
        for (int k=0; k < code.length(); k++) {
        if (code.charAt(k) >= 'a' && code.charAt(k) <= 'z') {
        int count = freqs[code.charAt(k)-'a']++;

        if (count > highestFreq) {
        highestFreq = count;
        highestFreqIdx = k;
        }
        }
        }



        1. Calculating the key can be simplified


        If I am not wrong, your key is the number of positions that the letter with the highest frequency is away from 'e'. In this case, you can simply do this:



        key = biggest - ('e' - 'a');


        So your code becomes:



        public static String decode (String code){
        int key = 0;
        final int ALPHABET_SIZE = 26;
        int freqs = new int[ALPHABET_SIZE];

        int highestFreq = 0;
        int highestFreqIdx = -1;
        for (int k=0; k < code.length(); k++) {
        if (code.charAt(k) >= 'a' && code.charAt(k) <= 'z') {
        int count = freqs[code.charAt(k)-'a']++;

        if (count > highestFreq) {
        highestFreq = count;
        highestFreqIdx = k;
        }
        }
        }

        key = highestFreqIdx - ('e' - 'a'); // Can also directly use 4 instead of 'e' - 'a'

        return (decode(code, key));
        }


        That said, your code will only work for messages that really have the letter 'e' as the most common letter...



        P/S Such code review questions may be better off in this Stack Exchange site for code reviews instead







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 16:13









        umop apisdnumop apisdn

        347615




        347615
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433812%2fcaesar-cipher-frequency-analysis-in-java%23new-answer', 'question_page');
            }
            );

            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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings