#include <iostream> #include <string> #include <stdlib.h> using namespace std; i
ID: 3560233 • Letter: #
Question
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
char message[100];
char output[100];
int key, count;
cout<<"Enter encrypted message:";
cin.getline(message,100);
for(key=1; key<=100; key++)
{
for(count=0; message[count]!=''; count++)
{
if(message[count]-key<32)
output[count]=message[count]-key-32+127;
else
output[count]=message[count]-key;
}
cout<<"Dencrypted message using key"<<key<<":";
cout<<output<<endl;
}
return 0;
}
Explanation / Answer
/*Provided comments for each of the line please check*/
#include <iostream.h>
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
//declaration of the array for input message
char message[100];
//declaration of the array for output message
char output[100];
//key,count are loop variables
int key, count;
//printing a string to the console
cout<<"Enter encrypted message:";
//reading a line from the console
cin.getline(message,100);
/*key variable is used in the outer loop to determine the 100 different possibilities of
encrypting the input message */
for(key=1; key<=100; key++)
{
/*count variable is used in the inner loop to iterate through input message to
detemine the each entription message*/
for(count=0; message[count]!=''; count++)
{
/*encrypting the message based on the 'key'(current number of iteration)*/
if(message[count]-key<32)/*if character at index 'count'(it takes ascii value) -key(current number of iteration)<32 */
output[count]=message[count]-key-32+127;//decryting
else/*else*/
output[count]=message[count]-key;//decryting
}
//printing the each decrypted message
cout<<"Dencrypted message using key"<<key<<":";
cout<<output<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.