ROT13 (rotate by 13 places) is a simple letter substitution cipher that is an in
ID: 3556189 • Letter: R
Question
ROT13 (rotate by 13 places) is a simple letter substitution cipher that is an instance of a Caesar cipher developed in ancient Rome and used by Julius Caesar who used it in his private correspondence. ROT13 replaces a letter with a letter 13 characters after it in the alphabet. The following table demonstrates the transliation in ROT13:
A <-> N
B <-> O
C <-> P
D <-> Q
E <-> R
F <-> S
G <-> T
H <-> U
I <-> V
J <-> W
K <-> X
L <-> Y
M <-> Z
Thus, the translation of the word JULIUS using ROT13 would be WHYVHF.
Write a C++ program that asks the user for the name of an input file and translates the contents of that input file using ROT13. Your main function should be responsible for reading the input file and coordinating calls to a value-returning function named Rot13 that will do the translation for each character and WriteTranslatedCharacter that will write the translated character to a secondary file. The Rot13 function takes as input the character to be translated and returns the translated character. The second function named WriteTranslatedCharacter will have two parameters, the translated character and a reference to an ifstream data type for a secondary file named
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
//The Rot13 function takes as input the character to be translated and returns the translated character.
char Rot13(char to_be_translated)
{
if(to_be_translated>='A' && to_be_translated<='Z')
{
return (to_be_translated-'A'+13)%26+'A';
}
else if(to_be_translated>='a' && to_be_translated<='z')
{
return (to_be_translated-'a'+13)%26+'a';
}
return to_be_translated;
}
// The second function named WriteTranslatedCharacter will have two parameters,
// the translated character and a reference to an ifstream data type for a secondary file named
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.