Substitution cipher

Report a typo

At some point in the Bioinformatics Institute, biology students no longer understood what did the computer science students say: they spoke a strange set of sounds.

And one of the biologists had suddenly discovered the secret of computer science students: they used the substitution cipher in their communication, i.e. they replaced each symbol of the initial message to another corresponding symbol. Biologists gained the key to the cipher and now they need help:

Write a program that can encode and decode the substitution cipher. The program accepts two input strings of the same length; the first line contains the characters of the original alphabet, the second line contains the symbols of a resulting alphabet. After that, there is a line you need to encode by the transmitted key, and another line to be decrypted.

For example, the program takes the following input:

abcd
*d%#
abacabadaba
#*%*d*%

It means that symbol a of the initial message is changed to symbol * in the cipher, b changed to d, c — to % and d — to #. You need to encode the string abacabadaba and decode the string #*%*d*% using this cipher. So you get the following lines, which should be the output of the program:

*d*%*d*#*d*
dacabac

Sample Input 1:

abcd
*d%#
abacabadaba
#*%*d*%

Sample Output 1:

*d*%*d*#*d*
dacabac

Sample Input 2:

dcba
badc
dcba
badc

Sample Output 2:

badc
dcba
Write a program in Java 17
class Main {
public static void main(String[] args) {
// put your code here
}
}
___

Create a free account to access the full topic