Write a C++ program for command-line arguments as follows FileCrypt -encrypt \"h
ID: 3532921 • Letter: W
Question
Write a C++ program for command-line arguments as follows
FileCrypt -encrypt "hello.txt" "hello.out"
FileCrypt -decrypt "hello.out" "hello.ans"
This program MUST be written using functions and arrays.
The program MUST and can ONLY contain the following function definitions and layout:
/**************************************************************
* This function displays the menu options as in the handout *
**************************************************************/
void displayMenu()
{ /* function body */ }
/**************************************************************
* This function encrypts the content of infile and saves the *
* encrypted text into outfile *
* @param infile string (file that has raw text) *
* @param outfile string (file that will have encrypted text) *
**************************************************************/
void encrypt(string infile, string outfile)
{ /* function body */ }
/**************************************************************
* This function decrypts the content of infile and saves the *
* decrypted text into outfile *
* @param infile string (file that has encrypted text) *
* @param outfile string (file that will have decrypted text) *
**************************************************************/
void decrypt(string infile, string outfile)
{ /* function body */ }
/**************************************************************
* This function takes an character and a cipher key to return *
* an encrypted character. *
* @param c is a char (the character to be encrypted) *
* @param key is an integer (cipher key given in the handout) *
**************************************************************/
char ceaserCipher(char c, int key)
{ /* function body */ }
/**************************************************************
* This function takes an encrypted character and a cipher key *
* to return a decrypted character. *
* @param c is a char (the character to be decrypted) *
* @param key is an integer (cipher key given in the handout) *
**************************************************************/
char ceaserDecipher(char c, int key)
{ /* function body */ }
/**************************************************************
* This is the main function of the program. *
* @return a value to terminate the program successfully *
**************************************************************/
int main()
{
displayMenu();
// 1. Get user input (e.g. encrypt/decrypt and filenames)
// 2. Call the appropriate function(s) to process encryption/decryption
// 3. Then, get user input to continue or exit the program
return 0;
}
Explanation / Answer
#include iostream
#include string.h
#include stdio.h
#include stdlib.h
#include conio.h
using namespace std;
int main()
{
char code[501];
int shift, len, i=0, j=0;
cout << "Caesar Cipher Decoder " << endl;
cout << " This program will decrypt the entered text using Caesar Cipher." << endl;
cout << " Press any key to continue...";
_getch();
system("cls");
cout << "Enter the text that has to be decrypted (max 500 characters):" << endl;
cin.getline(code, 501);
len = strlen(code);
while (j < len)
{
if(code[j] == 32)
{
code[j] = code[j];
}
j++;
}
po: cout << " Enter the amount of Caseser Shift in numbers: ";
cin >> shift;
if ((shift > 26) || (shift < 0))
{
cout << " Shift value should be less than or equal to 26. Type again." << endl;
goto po;
}
while (i < len)
{
code[i] = tolower(code[i]);
code[i] = code[i] - shift;
if (code[i] + shift == 32)
{
code[i] = code[i] + shift;
}
else if( ((code[i] + shift > 31) && (code[i] + shift < 65) || ((code[i] + shift > 90) && (code[i] + shift < 97)) || ((code[i] + shift > 122) && (code[i] + shift < 128))) )
{
code[i] = code[i] + shift;
}
else if (code[i] < 97)
{
if (code[i] == 32 - shift)
{
code[i] = code[i] + shift;
}
else
{
code[i] = (code[i] + 26);
}
}
i++;
}
system("cls");
cout << " Your deciphered code is: "" << code << """ << endl;
cout << " Your text has been decrypted." << endl;
cout << " Press any key to end." << endl;
_getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.