Programming Assignment 2 (100 Points): C++ Strings, References & Functions
Assignment Introduction:
Assignment 2 will extend the functionality from Assignment 1 and require that Assignment 1’s code be refactored to use functions with reference parameters. Refer to this handout for details about how to complete Assignment 2.
Refactoring and Adding Features to A1:
Use your code from Assignment 1 to write a program to encrypt and decrypt a plaintext (p) string of upper-case & lower-case letters, the digits 0-9 and two special characters, @,$. The plaintext will not contain spaces or other characters (e.g.#,etc.). Refer to the codebook array for all the allowable characters.
Just like in Assignment 1, your program will accept command line arguments to allow the user to enter in a key value k, a plaintext string of characters just described (−p) or a ciphertext (−C) and exactly one of the options to encrypt (−E) or decrypt (−D). Refer to the example usage on page 3 for how to run the program using the command line arguments and the skeleton code on page 4 for a general outline of the code.
Program Description and Functionality: Provide the following in your source file:
- Comments at the top of your .cpp file.
- A main function with command line arguments main(int argc, char* argv[])
- Use only C++ headers that place all standard-library routines in namespace For C++ strings use #include <string>; for standard input and output use #include <iostream> to access std::cout;std::cin; and use #include <cstring> for C-style string functions like std::strcmp and std::strlen and if needed #include<cstdlib> for helper functions like std::atoi, etc.
- The use of command line arguments (int argc, char* argv[]) to get the plaintext (-p sometext) or cipher text (-C zvtl0l40), the shift amount −k 7 and the options to encrypt (-E) or decrypt (-D).
- New for A2: The program needs to work for upper-case & lower-case letters, digits 0-9, & the symbols @ and $. Refer to the declaration of A1’s alphabet digits renamed to codebook for assignment 2:
char codebook[] =
{
’a’,’A’,’b’,’B’,’c’,’C’,’d’,’D’,’e’,’E’,’f’,’F’,’g’,’G’,’h’,’H’,’i’,’I’,
’j’,’J’,’k’,’K’,’l’,’L’,’m’,’M’,’n’,’N’,’o’,’O’,’p’,’P’,’q’,’Q’,’r’,’R’,
’s’,’S’,’t’,’T’,’u’,’U’,’v’,’V’,’w’,’W’,’x’,’X’,’y’,’Y’,’z’,’Z’,’0’,’1’,
’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’,’@’,’$’
};
- New for A2: Write at least three functions, encrypt(), decrypt(), & copy_string() with the following function prototypes:
// | ||||
// where plaintext and k are the values parsed// and s is a reference to a string// void encrypt(std::string& plaintext, int k);// | from | the | command | line |
// where ciphertext and k are the values parsed// and s is a reference to a string// void decrypt(std::string& ciphertext, int k);//// function to copy a cstring to a C++ string | from | the | command | line |
// s is a reference to either the plaintext or// text is the command line cstring from argv// void copy_string(std::string& s, char* text); | ciphertext string |
- To encrypt a plaintext string use the following formula to compute an index i into the codebook with length #define CBL 64 //Code Book Length:
Ci = E(p) = (p + k) mod CBL,
where mod is the modulus operator %, C is the ciphertext, E is the encryption function, p is the index of the plaintext character, k is the key shift, and CBL is the length of the codebook array.
- To decrypt a ciphertext string use the following formula to compute an index i into the codebook array:
pi = D(C) = (C − k) mod CBL, (C − k) >= 0 pi = D(C) = (((C − k) mod CBL)+ CBL) mod CBL, (C − k) < 0
where mod is the modulus operator %, C is the index of the ciphertext character, D is the decryption function, p is the plaintext, k is the key shift, and CBL is the length of the codebook array.
In C & C++, mod, or remainder of division, is the % symbol. For negative C − k numbers refer to the references on page 4 for more details about the modulus operator in C++ with negative numbers.
Example usage:
>A02.exe -p @Bc -k 3 -EThe plaintext was: @Bc The ciphertext is: AdD enter any key to exit | //encrypt the string @Bc |
>A02.exe -C AdD -k 3 -DThe ciphertext was: AdD The plaintext is: @Bc enter any key to exit | //decrypt the string AdD |
>A02.exe -k 5 -p 7$9 -EThe plaintext was: 7$9 The ciphertext is: acb enter any key to exit | //encrypt the string 7$9 |
>A02.exe -C acb -k 5 -DThe ciphertext was: acb The plaintext is: 7$9 enter any key to exit | //decrypt the string acb |
>A02.exe -p sometext -k 7 -EThe plaintext was: sometext The ciphertext is: VRPHWH1W enter any key to exit | //encrypt the string sometext |
>A02.exe -C VRPHWH1W -k 7 -DThe ciphertext was: VRPHWH1W The plaintext is: sometext enter any key to exit | //decrypt the string VRPHWH1W |
>A02.exe -k 9 -E -p tH1s1sv3ryYc00l //encrypt the string tH1s1sv3ryYc00l
The plaintext was: tH1s1sv3ryYc00l The ciphertext is: [email protected]@WZaV56G99P enter any key to exit
>A02.exe -C [email protected]@WZaV56G99P -k 9 -D //decrypt the string [email protected]@WZaV56G99P
The ciphertext was: [email protected]@WZaV56G99P The plaintext is: tH1s1sv3ryYc00l enter any key to exit
Skeleton code
// | |
// Name: Your First Name & Last// SSID: Student ID Number// Assignment #: 2// Submission Date: 10/3/2020 | Name |
//
// Description: A program to encrypt and decrypt a message using a shift cipher
// The plaintext message must only contain lowercase alpha and digits 0-9
//
// command line arguments
// -p theplaintextmessage1 – the plaintext (p) message to be encrypted
// -C 2qnyujrw2n62vn11jpna – the cipher (C) text to be decrypted
// -k 9 – the key (k) shift value
// -E – the option to encrypt (E)
// -D – the option to decrypt (D)
//
#include <iostream> //std::cout, std::cin, etc.
#include <cstdlib> //C++ version of stdlib.h
#include <cstring> //C++ version of string.h #include <string>
// Todo A2: encrypt using std::string void encrypt(std::string& plaintext, int k);
// Todo A2: decrypt using std::string void decrypt(std::string& ciphertext, int k);
// Todo A2: helper function to copy a cstring to an std::string
void | copy_string(std::string& s, char* text); |
int{ | main(int argc, char *argv[])// Example variables for A02int k = 0; // k shiftint msg_index = 0; // msg_index of plaintext or ciphertext in argv std::string msg; // string to hold the plaintext or ciphertext; bool do_encrypt = false; //True for encrypt or False for decrypt//// A1: process the command line arguments//// Todo A2: copy argv into msg using copy_string function |
//
// Todo A2: call encrypt or decrypt functions
//
if (do_encrypt) //encrypt plaintext
encrypt(msg, k);
else decrypt(msg, k);//decrypt ciphertext
//
// Todo A2:
// print the original message and the plaintext | ciphertext
//
return 0; }//main
Where to do the assignment
You can do this assignment on your own computer, or using the SMC Virtual labs with Citrix. In either case, ensure the code compiles and runs on Windows. Submit one .cpp file named A02.cpp. Do not use any other name or else points will be deducted.
Submitting the Assignment
Include your name, your student id, the assignment number, the submission date, and a program description in comments at the top of your files, and use the CS52 Programming Guide for coding style guidance. Submit the assignment on Canvas (https://online.smc.edu) by uploading your .cpp file to the Assignment 2 entry as an attachment. Do not cut-and-paste your program into a text window. Do not hand in a screenshot of your program’s output. Do not hand in a text file containing the output of your program.
Saving your work
Save your work often on a flash-drive or to the cloud (e.g., GoogleDrive, Microsoft OneDrive, Canvas, etc.). Always save a personal copy of your files (e.g. .cpp, .h, etc.). Do not store files on the virtual lab computers.
References:
Microsoft C++ Language Reference
ISO C++ Standard Library and other refs
’The modulus operator yields the remainder given by the following expression, where e is the first operand and e2 is the second: e1 − (e1/e2) ∗ e2, where both operands are of integral types.’ Microsoft C++ Modulus & Multiplication Operators
Reviews
There are no reviews yet.