[URI ONLINE JUDGE] – 1249 – Rot13

Write a program that receives a secret message, and returns this coded message. The message is encrypted with a cipher called rot13. Rot13 works as follows, for each letter is assigned an index, based on its position in the alphabet. a = 1, b = 2, c = 3, …, z = 26. Each letter of the message should be moved 13 places to the right, if the end of the alphabet is reached, the count starts at the first character of the alphabet. For example, the character “a” (quotes only to exemplify) would be mapped to “n”, “y” to “l”, “f” to “s”, and so on. The same process is also made to uppercase. Any non-alphabetic characters must be transferred to the output unchanged.

Input

The input consist in many test cases. Each test case is composed by a line that contains a message with at least 1 up to 50 characters including, containing letters, numbers, and the following symbols: !@#$%^&*()-_=+[]{}|;’:,./<>?”

Output

For each line of input, print a line in the output with the message encoded in the rot13 method.

Sample Input Sample Output
z
a
x
Hello world!
This is a test message.
abc123<>,xyz?*$
m
n
k
Uryyb jbeyq!
Guvf vf n grfg zrffntr.
nop123<>,klm?*$

My solution:


#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

char l[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklm";
char u[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM";

char rot13(char c){
int i;

for (i = 0; i <= 25; i++){
if (c == l[i]){
return l[i + 13];
}
}
for (i = 0; i <= 25; i++){
if (c == u[i]){
return u[i + 13];
}
}
return c;
}

int main(){
char c;
while((c = getchar()) != EOF){
cout << rot13(c);
}
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