Spell Decoder

Modules - CSE2120 Data Structures, CSE2140 Files

 

Teacher’s Notes:

=======================================

The key for decoding the sample input below is found at 

https://github.com/TaoOfChow/ClassroomSupportFiles/blob/main/spellDecoder.txt

You will need to copy it somewhere for the students to access or you can keep the link to the file.

 
The code for a random code generator is provided below.
 
Making a program to devise your own random code could be another assignment.
 

Assignment:

=======================================

Magic Spell Decoder  

Merlin is a wizard who has decided to use a simple cypher to prevent his rivals from stealing his spells.  To do this, he has replaced each letter of the alphabet with another letter.  Each letter is only replaced with one other letter and once a letter is substituted, it cannot be used as a substitute for another letter.  For example, if he substitutes 'a' with 'z', then 'a' cannot be substituted with any other letter. Also, 'z' could not be used as a substitute for any other letter.
 
You have come into possession of Merlin's cypher key.  Write a program that will open the file "key.txt".  This file is provided at
 
 
This file stores the replacement values for each letter in the alphabet in alphabetical order.  The first letter in the file is the replacement for 'a', the second letter is the replacement for 'b', etc.  
 
This program will then prompt the user to enter the coded spell name as a String from the keyboard. This input will always be made from only lower case letters and spaces. The program will not convert spaces but will include them in the same position in the decoded text.  
 
It will then use the key to decode the message to its original form. Once this is done, the program will print the decoded spell to the screen.  Keep in mind that this String might have multiple words in it separated by spaces.  
 

Sample Input 1:

hlulknyi uymnid xyyi

 

Sample Output 1:


vacation coming soon

 

 

 

Solution (JAVA):

=======================================

import java.io.File;
import java.util.Scanner;
 
public class Decoder {
    
    public static char translate(char ch, char[] key) {
        for(int x = 97; x < 123; x++)
            if(key[x] == ch)
                return (char)x;
        return 32;
    }
    
    public static void main(String[] args)throws Exception {
        
        char[] key = new char[123];
        Scanner input = new Scanner(new File("key.txt"));
        
        for(int x = 97; x < 123; x++)
            key[x] = input.next().charAt(0);
        
        Scanner message = new Scanner(new File("message.txt"));
        String st = message.nextLine();
        
        System.out.println(st);
        String decodedMessage = "";
        for(int x=0; x < st.length(); x++){
            decodedMessage += translate(st.charAt(x),key);
        }
        
        System.out.println(decodedMessage);
    }
}
 

 

Code Maker (JAVA):

=======================================

 
import java.io.File;
import java.io.PrintWriter;
 
public class CodeMaker {
    public static void main(String[] args) throws Exception {
        File outfile = new File("secretCode.txt");
        PrintWriter pw = new PrintWriter(outfile);
        
        char[] legend = new char[26];
        char currentLetter = 'a';
        
        //populating the legend with the alphabet characters sequentially.
        for(int x = 0; x < 26; x++)
            legend[x] = currentLetter++;
        
        //shuffling the legend
        char temp = ' ';
        for(int x = 0; x < 26; x++) {
            int randomIndex = (int)(Math.random()*26);
            temp = legend[x];
            legend[x] = legend[randomIndex];
            legend[randomIndex] = temp;
        }
        
        for(int x = 0; x < 26; x++)
            pw.println(legend[x]);
        
        pw.close();
    }
}