Editorial for Trivial Encryption


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Penguin60

C++

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string s1, s2; cin >> s1 >> s2;

    for (int i = 0; i < s1.size(); i++) {
        int offset = s2[i % s2.size()] - 'A';
        int current = s1[i] - 'A';
        int final = (current + offset)%26;

        s1[i] = final + 'A';
    }

    cout << s1 << "\n";
}

Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String s1 = scanner.next();
        String s2 = scanner.next();

        StringBuilder result = new StringBuilder();

        for (int i = 0; i < s1.length(); i++) {
            int offset = s2.charAt(i % s2.length()) - 'A';
            int current = s1.charAt(i) - 'A';
            int finalChar = (current + offset) % 26;

            result.append((char) (finalChar + 'A'));
        }

        System.out.println(result.toString());
    }
}

Python

s1 = input().strip()
s2 = input().strip()

s1 = list(s1)

for i in range(len(s1)):
    offset = ord(s2[i % len(s2)]) - ord('A')
    current = ord(s1[i]) - ord('A')
    final = (current + offset) % 26

    s1[i] = chr(final + ord('A'))

print("".join(s1))

Comments

There are no comments at the moment.