Goals: Developing problem-solving skills and functions, using character arrays a
ID: 3547595 • Letter: G
Question
Goals: Developing problem-solving skills and functions, using character arrays and .get
Problem: The Caesar Cipher is a simple letter substitution cipher (cipher: algorithm for performing encryption or decryption) that was developed in ancient Rome and used by Julius Caesar to communicate to his army. ROT13 is one instance of this cipher, and it replaces a letter with the letter 13 letters after it in the alphabet:
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.
In this assignment, you will write a program that will allow for all Caesar Ciphers, using all rotation values (13 being an example above, from ROT13). You will also allow for rotation from both the left and right.
You will place a message, up to 200 characters (all in lowercase), in an input file. Your main() function shall read data from the file. Since this message may include spaces and newlines, we will use .get to read every character. Your program will also have 7 functions:
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
void printmsg(char *msg,char *filename)
{
std::cout<<" "<<msg;
std::ofstream ofs(filename, std::ofstream::out);
ofs<<msg;
ofs.close();
}
char* readfile(char *filename)
{
char msg[200];
int i=0;
std::ifstream is(filename); // open file
while (is.good()) // loop while extraction from file is possible
{
msg[i] = is.get();
i++; // get character from file
}
msg[i]='';
is.close();
return msg;
}
char rotate_letter(char c,int rot)
{
rot=rot%26;
int a=c;
if(a>=97 && a<=122)
{
a=a+rot;
if(a>122)
a=a-26;
else if(a<97)
a=a+26;
}
else if(a>=65 && a<=90)
{
a=a+rot;
if(a>90)
a=a-90+64;
else if(a<65)
a=a-64+90;
}
c=a;
return c;
}
char* rotate_left(char *msg,int size,int rotamt)
{
char encp[size+1];
int i;
for (i=0;i<=size;i++)
{
if(msg[i]==' ' || msg[i]==' ' || msg[i]=='')
{
encp[i]=msg[i];
}
else
{
encp[i]=rotate_letter(msg[i],-rotamt);
}
}
encp[i]='';
return encp;
}
char* rotate_right(char *msg,int size,int rotamt)
{
char encp[size+1];
int i;
for (i=0;i<=size;i++)
{
if(msg[i]==' ' || msg[i]==' ' || msg[i]=='')
{
encp[i]=msg[i];
}
else
{
encp[i]=rotate_letter(msg[i],rotamt);
}
}
encp[i]='';
return encp;
}
char* caesar_cipher(char *msg,int size,int rotamt,char *rotdir)
{
if(rotdir=="left")
{
return rotate_left(msg,size,rotamt);
}
else if(rotdir=="right")
{
return rotate_right(msg,size,rotamt);
}
}
char* caesar_cipher(char *msg,int size,char *rotdir)
{
int rotamt=13;
if(rotdir=="left")
{
return rotate_left(msg,size,rotamt);
}
else if(rotdir=="right")
{
return rotate_right(msg,size,rotamt);
}
}
main()
{
char *msg=readfile("ip1.txt");
char *encrypted=caesar_cipher(msg,5,"left");
printmsg(encrypted,"op1.txt");
msg=readfile("op1.txt");
char *decrypted=caesar_cipher(msg,5,-13,"right");
cout<<decrypted;
msg=readfile("ip2.txt");
encrypted=caesar_cipher(msg,5,8,"left");
printmsg(encrypted,"op2.txt");
msg=readfile("op2.txt");
decrypted=caesar_cipher(msg,5,-8,"right");
cout<<decrypted;
msg=readfile("ip3.txt");
encrypted=caesar_cipher(msg,5,20,"left");
printmsg(encrypted,"op3.txt");
msg=readfile("op3.txt");
decrypted=caesar_cipher(msg,5,-20,"right");
cout<<decrypted;
msg=readfile("ip4.txt");
encrypted=caesar_cipher(msg,5,36,"left");
printmsg(encrypted,"op4.txt");
msg=readfile("op4.txt");
decrypted=caesar_cipher(msg,5,-36,"right");
cout<<decrypted;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.