How to resolve the algorithm Straddling checkerboard step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Straddling checkerboard step by step in the Java programming language

Table of Contents

Problem Statement

Implement functions to encrypt and decrypt a message using the straddling checkerboard method. The checkerboard should take a 28 character alphabet (A-Z plus a full stop and an escape character) and two different numbers representing the blanks in the first row. The output will be a series of decimal digits. Numbers should be encrypted by inserting the escape character before each digit, then including the digit unencrypted. This should be reversed for decryption.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Straddling checkerboard step by step in the Java programming language

The code takes a string and encodes it using a straddling checkerboard, and then decodes the encoded string.

A Straddling checkerboard is a cipher that uses a 5x5 grid to encode a message. The grid is filled with the alphabet, with some letters being omitted. To encode a message, the message is written in rows across the grid, and then the columns are read out to get the encoded message.

The code uses two maps, val2key and key2val, to convert between the letters and their corresponding values on the grid. The map val2key maps each letter to its value on the grid, and the map key2val maps each value on the grid to its corresponding letter.

The encode function takes a string as input and converts it to uppercase. It then splits the string into individual characters and iterates over each character. For each character, the function looks up its value on the grid using the val2key map. If the character is not found on the grid, the function ignores it. Otherwise, the value of the character is appended to the output string.

The decode function takes an encoded string as input. It uses a regular expression to find all of the values on the grid in the encoded string. For each value, the function looks up its corresponding letter using the key2val map. If the value is not found on the grid, the function ignores it. Otherwise, the letter corresponding to the value is appended to the output string.

The following is an example of how the code can be used to encode and decode a message:

String s = "Hello world";
String encoded = encode(s);
String decoded = decode(encoded);
System.out.println(decoded); // prints "Hello world"

Source code in the java programming language

import java.util.HashMap;
import java.util.Map;
import java.util.regex.*;

public class StraddlingCheckerboard {

    final static String[] keyvals = {"H:0", "O:1", "L:2", "M:4", "E:5", "S:6",
        "R:8", "T:9", "A:30", "B:31", "C:32", "D:33", "F:34", "G:35", "I:36",
        "J:37", "K:38", "N:39", "P:70", "Q:71", "U:72", "V:73", "W:74", "X:75",
        "Y:76", "Z:77", ".:78", "/:79", "0:790", "1:791", "2:792", "3:793",
        "4:794", "5:795", "6:796", "7:797", "8:798", "9:799"};

    final static Map<String, String> val2key = new HashMap<>();
    final static Map<String, String> key2val = new HashMap<>();

    public static void main(String[] args) {
        for (String keyval : keyvals) {
            String[] kv = keyval.split(":");
            val2key.put(kv[0], kv[1]);
            key2val.put(kv[1], kv[0]);
        }
        String enc = encode("One night-it was on the twentieth of March, "
                + "1888-I was returning");
        System.out.println(enc);
        System.out.println(decode(enc));
    }

    static String encode(String s) {
        StringBuilder sb = new StringBuilder();
        for (String c : s.toUpperCase().split("")) {
            c = val2key.get(c);
            if (c != null)
                sb.append(c);
        }
        return sb.toString();
    }

    static String decode(String s) {
        Matcher m = Pattern.compile("(79.|3.|7.|.)").matcher(s);
        StringBuilder sb = new StringBuilder();
        while (m.find()) {
            String v = key2val.get(m.group(1));
            if (v != null)
                sb.append(v);
        }
        return sb.toString();
    }
}


  

You may also check:How to resolve the algorithm Longest common substring step by step in the Ada programming language
You may also check:How to resolve the algorithm Cholesky decomposition step by step in the Rust programming language
You may also check:How to resolve the algorithm Circular primes step by step in the Perl programming language
You may also check:How to resolve the algorithm Y combinator step by step in the Vim Script programming language
You may also check:How to resolve the algorithm String prepend step by step in the Wart programming language