82.9k views
3 votes
IN JAVA CODE

The enemy has been sending encoded messages. One of our cryptographers finally figured out the system. The enemy sends a series of strings, where the length of each string and the number of strings are the same. The actual message is the letters on a diagonal from the first letter of the last string to the last letter of the first string. For example, 'ATTACK' is encoded below:

STREAK
STOICS
AFRAID
SETTLE
STAIRS
ARRIVE

Prompt the user for the first string. That will tell you the number of strings to expect. Then prompt the user for the other strings. Store the strings in a 2-dimensional array of characters (you must use an array, not a series of strings). You can assume a maximum string size (# of strings) of 10 and no space, digits, or punctuation in the strings (i.e. the strings will be alphabetic characters). The user may enter upper or lower case, but you should report results in all upper case.
Once the user has entered all the input strings, you will print out the secret message (the characters on the diagonal).
Name your class program3b.

1 Answer

5 votes

Final answer:

To decode a message in Java using a transposition cipher technique, prompt the user for strings, store them in a 2D char array, and extract the diagonal to reveal the secret message. The code provided exemplifies this process and outputs the result in uppercase.

Step-by-step explanation:

In Java code, to decode a message sent using a similar system to a transposition cipher, you can prompt the user to input the encoded strings. You then store these strings in a two-dimensional character array and use the diagonal pattern described to extract the secret message. The code example below demonstrates how you can implement this in Java:

import java.util.Scanner;

public class program3b {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first string:");
String first = scanner.nextLine().toUpperCase();
int size = first.length();
char[][] encoded = new char[size][size];

for (int i = 0; i < size; i++) {
if (i > 0) { // We already have the first string
System.out.println("Enter next string:");
String next = scanner.nextLine().toUpperCase();
encoded[i] = next.toCharArray();
} else {
encoded[i] = first.toCharArray();
}
}

System.out.println("The secret message is:");
for (int i = 0; i < size; i++) {
System.out.print(encoded[size - 1 - i][i]);
}
}
}

When you run this code, it will prompt for the required number of string inputs, store them in an array, and then output the decoded message, which is composed of the characters along the diagonal from the bottom-left corner to the top-right corner of the 2D array.

User LAW
by
8.6k points