[URI Online Judge] – 1024 – Encryption

You have been asked to build a simple encryption program. This program should be able to send coded messages without someone been able to read them. The process is very simple. It is divides into two parts.

First, each uppercase or lowercase letter must be shifted three positions to the right, according to the ASCII table: letter ‘a’ should become letter ‘d’, letter ‘y’ must become the character ‘|’ and so on. Second, each line must be reversed. After being reversed, all characters from the half on (truncated) must be moved one position to the left in ASCII. In this case, ‘b’ becomes ‘a’ and ‘a’ becomes ‘`’.

For example, if the resulting word of the first part is “tesla”, the letters “sla” should be moved one position to the left. However if the resulting word of the first part is “t#$A”, the letters “A$” are to be displaced.

Input

The input contains a number of test cases. The first line of each test case contains an integer N (1 ≤ N ≤ 1 * 10⁴), indicating the number of lines the problem should encrypt. The following N lines contains Mcharacters each M (1 ≤ M ≤ 1 * 10³).

Output

For each test case, you must present the encrypted message.

Sample Input Sample Output
4
Texto #3
abcABC1
vxpdylY .ph
vv.xwfxo.fd
3# rvzgV
1FECedc
ks. \n{frzx
gi.r{hyz-xx

my solution:


#include <iostream>
// #include <cstdio>

using namespace std;

int main(){
int n;
string s;

cin >> n;
getline(cin, s);

while(n--){
getline(cin, s);

string r(s.rbegin(), s.rend());

unsigned int l = r.length();

for (unsigned int i = 0; i < s.length(); ++i){
if ((r.at(i) >= 65 && r.at(i) <= 90) || (r.at(i) >= 97 && r.at(i) <= 122)){
r.at(i) = r.at(i) + 3;
}
if (i >= l / 2){
r.at(i) = r.at(i) - 1;
}
}

cout << r << endl;
}

return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s