Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

pter 8, we learned about a simple encryption n algorithm called a Caesar cipher

ID: 3791704 • Letter: P

Question



pter 8, we learned about a simple encryption n algorithm called a Caesar cipher write a program n the language of your choice to implement the Caesar cipher algorithm 8 write a main function (method, procedure that collects a message from the user and writes it out again. Assume for simplicity that the message contains no more than 10 characters and that only the 96 uppercase letters of the aphabet are used. Use an array of 10 elements to store the message. Ask the user to enter no more than 10 characters, one per line, and to terminate the message by entering some special character such as use a variable to keep track of the number of array elements actually used (which could be fewer than 10 if the message is short) so that you do not write out meaningless characters stored at the end of the array tthr contents of

Explanation / Answer

The question is divided into two parts :

Before programming the solution, you must understand the Caesar Algorithm in nutshell.

Caesar Algorithm is simply a type of substitution cipher, i.e., each letter of a given text is replaced by a letter some fixed number of positions down the alphabet. For example with a shift of 1, A would be replaced by B, B would become C, and so on.
Thus to cipher a given text we need an integer value, known as shift which indicates the number of position each letter of the text has been moved down.

The full program is as follows (in C++):



#include <iostream>

using namespace std;

// This function receives text and shift and returns the encrypted text

string encrypt(char input[], int s)

{

string result = "";

// traverse text
int len = sizeof(&input)/sizeof(input[0]); // To know the length of the array

for (int i=0;i<len;i++)

{

// apply transformation to each character

result += char(int(input[i]+s-65)%26 +65);
}

// Return the resulting string

return result;

}

// Driver program to test the above function

int main()

{

char input[10];

int arrayCount = 0;

cout<<"Enter the Character Message Array"<<endl;

for(int i=0; i<10; i++){

arrayCount++;

cin>>input[i];

if(input[i] == '%'){

break;

}

}

cout<<"Enter Shift number"<<endl;

int shift;

cin>>shift;

cout << " Cipher: " << encrypt(input, shift);

return 0;

}