The Key to Cryptography

From programming_contest
Jump to navigation Jump to search

This problem asks us to rotate the characters in a string by the value of the character in the same position in a key string.

If you use ascii math and char arrays this is easy.

encrypt[i] = (((plain[i] - 'A') + (key[i] - 'A')) %26) + 'A'

plain[i] = (((encrypt[i] - 'A') - (key[i] - 'A') + 26) % 26) + 'A'

be sure to add 26 when modding after doing a subtraction.