In: Computer Science
In this lab you will be encrypting a message. It is a simple encryption described in the problem. To program it remember that the ASCII code for ‘A’ is 65 and the other capital letters are in order 66, 67, 68, … The ASCII code for ‘a’ is 97 and the lowercase continues 98, 99, and so on. The hint is that a if a user type in a char with an ASCII code between 65 and 90, or 97 and 122 then add 5 and print out the result. Otherwise, have a sequence of if-statements to encrypt the punctuation marks as given in the problem. Keep looping until the character typed in is a ‘#’ symbol.
1. Write a C++ program that encrypts a message. The program should read in a character, “encrypt” the character by giving the numeric place the character has in the alphabet then add 5. The program should then printout the encrypted message to the screen. The program should continue taking in characters until the user types in a # symbol. Punctuation should be handled as follows: . A , B ? C ! D. No other characters will be encrypted.
Following is the code for the problem:
#include<iostream>
using namespace std;
int main()
{
cout<<"Enter a character; enter # to exit.\n";
while(true)
{
char c;
cin>>c;
if(c=='#')
break;
cout<<"Encrypted message: ";
if((c>=65&&c<=90)||(c>=97&&c<=122))
cout<<((int)c+5)<<endl;
else if(c=='.')
cout<<"A\n";
else if(c==',')
cout<<"B\n";
else if(c=='?')
cout<<"C\n";
else if(c=='!')
cout<<"D\n";
else
cout<<"Invalid character.";
}
return 0;
}
Following are the outputs for sample inputs: