In: Computer Science
Write a program in c++, with at least four functions, including main, which must do the following:
Input will be a string of no more than 25 characters. Blanks get replaced with blanks.
Do not worry about punctuation; there will be no punctuation in the string.
ALPHABET becomes NYCUNORG
Test your program with the following strings:
TAF VF
paddrpf
Strings Will be used to test code!
Some suggestions (NOT requirements):
Decryption Key
A|B|C|D|E|F|G|H|I|J|K|L|M
-------------------------
N|O|P|Q|R|S|T|U|V|W|X|Y|Z
(letter above equals below, and vice versa)
As you can see, A becomes N, B becomes O and so on.
Main
-user input processing -repeat if wanted
-clean up of dynamic storage (delete)
|
_____________________________ |____________________________
| | |
Input encrypt/decrypt output
-read string -read from dynamic storage -get results
-put into dynamic storage -encrypt/decrypt -output results
-store in dynamic storage
C++ Program:
/* C++ Program that decrypts the given cipher text by left rotating by 13 */
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;
//Function prototype
char* decrypt(char*);
char* encrypt(char*);
char* inputString();
//Main function
int main()
{
int opt;
char *str, *opstr;
while(1)
{
//Printing menu
cout << "\n\n ***** MENU ***** \n 1-Encryption \n
2-Decryption \n 3-Exit \n\n Your option: ";
cin >> opt;
if(opt == 1)
{
//Reading string
str = inputString();
opstr = encrypt(str);
cout << "\n Encrypted: " << opstr;
delete[] str;
}
else if(opt == 2)
{
//Reading string
str = inputString();
opstr = decrypt(str);
cout << "\n Decrypted: " << opstr;
delete[] str;
}
else
{
return 0;
}
}
return 0;
}
//Function that reads the string
char* inputString()
{
char *str;
//Allocating memory
str = new char[25];
cin.ignore();
cout << "\n Enter String: ";
cin.getline(str, 100);
return str;
}
//Function that decodes given message using left rotational
shift key
char* decrypt(char* cipher)
{
int i,j;
//Shift value 13
int val, shift=13;
//Character array
char *decodedMesg = new char[25];
//Looping over each character
for(i=0; i<strlen(cipher); i++)
{
//Getting Ascii values
val = (int)(cipher[i]);
//For Upper case letters
if(val >= 65 && val <= 90)
{
//Boundary crossing condition
if( (val - shift) < 65 )
val = 90 - abs(( (val - shift) - 65 )) + 1;
else
val = val - shift;
//Decrypting
decodedMesg[i] = (char)(val);
}
//For Lower case letters
else if(val >= 97 && val <= 122)
{
//Lower case
if( (val - shift) < 97 )
val = 122 - abs(( (val - shift) - 97 )) + 1;
else
val = val - shift;
//Decrypting
decodedMesg[i] = (char)(val);
}
//Other than letters
else
decodedMesg[i] = cipher[i];
}
//Adding string termination character
decodedMesg[i] = '\0';
//Returning the plain text
return decodedMesg;
}
//Function that decodes given message using left rotational shift
key
char* encrypt(char* plain)
{
int i,j;
//Shift value 13
int val, shift=13;
//Character array
char *decodedMesg = new char[25];
//Looping over each character
for(i=0; i<strlen(plain); i++)
{
//Getting Ascii values
val = (int)(plain[i]);
//For Upper case letters
if(val >= 65 && val <= 90)
{
//Boundary crossing condition
if( (val + shift) > 90 )
val = 65 + abs(( 90 - (val + shift) )) - 1;
else
val = val + shift;
//Decrypting
decodedMesg[i] = (char)(val);
}
//For Lower case letters
else if(val >= 97 && val <= 122)
{
//Lower case
if( (val + shift) > 122 )
val = 97 + abs(( 122 - (val + shift) )) - 1;
else
val = val + shift;
//Decrypting
decodedMesg[i] = (char)(val);
}
//Other than letters
else
decodedMesg[i] = plain[i];
}
//Adding string termination character
decodedMesg[i] = '\0';
//Returning the plain text
return decodedMesg;
}
____________________________________________________________________________________________
Sample Run: