Click here to Skip to main content
15,891,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The goal is to calculate the frequency of the ciphertext letters and then replace them with English letters based on their frequency statistics.

What I have tried:

I have generated the frequencies but when I'm using the replace method it's not giving the desired output, for example, if I use one replace() method, it outputs accordingly(
String A = encryptedText.replace('a','z');
. It replaces all the 'a's with 'z's but if I add the replace methods(one replace method for each character of the alphabet for example
(<pre>  String B = encryptedText.replace('b','y');
,
(<pre>  String C = encryptedText.replace('c','x');
) and so on only the first method replace function is executed, the others are not executed and it doubles all the letters in the output, for example, if I replace 'a','b' and 'c' with 'z','y' and 'x' respectively, it outputs as "
zbcaycabxabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc
".

Below is my code:

import java.util.Arrays;
import java.util.Scanner;

public class FrequencyAnalysisCaesarCipher {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter message to be decrypted: ");
        String encryptedText = input.nextLine().toLowerCase();

        int[] freq = new int[encryptedText.length()];
        int i = 0;
        int j;

        //Converts given string into character array
        char[] string = encryptedText.toCharArray();
        Arrays.sort(string);
        Arrays.sort(new int[]{freq[i]});

        for (i = 0; i < encryptedText.length(); i++) {
            freq[i] = 1 ;
            for (j = i + 1; j < encryptedText.length(); j++) {
                if (string[i] == string[j]) {
                    freq[i]++;

                    //Set string[j] to 0 to avoid printing visited character
                    string[j] = '0';

                }
            }
        }

        //Displays each character and their corresponding frequency
        System.out.println("Characters and their corresponding frequencies");
        for (i = 0; i < freq.length; i++) {
            if (string[i] != ' ' && string[i] != '0') {
                System.out.println(string[i] + "-" + freq[i]);
            }
        }
        System.out.println("..............................................");

        // oldChar is cipher text letter and newChar is English letter
        // The newChar have been initiated just so to run the program but can be replaced accordingly based on frequency and then re-run to give the desired output

        String A = encryptedText.replace('a','z');
        String B = encryptedText.replace('b','y');
        String C = encryptedText.replace('c','x');
        String D = encryptedText.replace('d','w');
        String E = encryptedText.replace('e','v');
        String F = encryptedText.replace('f','u');
        String G = encryptedText.replace('g','t');
        String H = encryptedText.replace('h','s');
        String I = encryptedText.replace('i','r');
        String J = encryptedText.replace('j','q');
        String K = encryptedText.replace('k','p');
        String L = encryptedText.replace('l','o');
        String M = encryptedText.replace('m','n');
        String N = encryptedText.replace('n','m');
        String O = encryptedText.replace('o','l');
        String P = encryptedText.replace('p','k');
        String Q = encryptedText.replace('q','j');
        String R = encryptedText.replace('r','i');
        String S = encryptedText.replace('s','h');
        String T = encryptedText.replace('t','g');
        String U = encryptedText.replace('u','f');
        String V = encryptedText.replace('v','e');
        String W = encryptedText.replace('w','d');
        String X = encryptedText.replace('x','c');
        String Y = encryptedText.replace('y','b');
        String Z = encryptedText.replace('z','a');

        System.out.print(A+B+C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z);


    }
}
Posted
Updated 24-Apr-20 17:24pm

Objective
The goal is to calculate the frequency of the ciphertext letters and then replace them with English letters based on their frequency statistics.
You are completely on the wrong track for this assignment; all you are doing is replacing the letters in with other letters, and you messed that up as well.

The first part of this task is going to be determining the frequency statistics of each character appearing in the provided string. Think of this as a data table looking like this
Chr Cnt
 a   14
 b   3
 c   7
 d   11

and so on...
And once that is done, you then can sort that table based on count and do your replacements based on this order.
 
Share this answer
 
The first replacement string (A) should be the input to the next call:
C#
        String A = encryptedText.replace('a','z');
        String B = A.replace('b','y');
        String C = B.replace('c','x');
// etc .

The final string should then contain all the replacements.
 
Share this answer
 
Comments
Richard Deeming 24-Apr-20 13:37pm    
Surely that wouldn't work as intended?
encryptedText
    .replace('a', 'z')
    ...
    .replace('z', 'a')

You'll end up replacing n..z, but leave a..m intact.
Richard MacCutchan 25-Apr-20 3:59am    
Doh!
Richard MacCutchan 25-Apr-20 3:59am    
Doh!
Quote:
when I'm using the replace method it's not giving the desired output

For encrypting and decrypting, you have to build your own replace function.
I don't Java, but here is the principle:
Rather than doing 1 replace at the time on whole string, you have to handle 1 char at the time and do the correct replace on it.
Here is the principle in pseudo code:
Java
// get message
msg= " My message to code"
// define the key
Key="zyxwvutsrqponmlkjihgfedcba"
// loop on message
for i=0 to msg.length-1
    // get nth char
    tmp= msg[i]
    // check if lowercase letter
    if tmp is lowercase letter
        // convert to position in key
        pos= tmp- 'a'
        // replace
        msg[i]= key[pos]
    endif
next
// message is encrypted
return msg
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900