Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#include <cstring>
#include <cctype>
/*
* Description: This program operates on a command-line argument. It
includes functions
* that implement a simple Caesar or Substitution cipher. Given key k
and message m, the Caesar
* cipher will "shift" each letter in m k letters to the right using a
standard english alphabet.
* The main program will take in a operation int, a key, and a message
(either plaintext or
* encrypted. It will then output the message after it has been
encrypted (operation 0) or
* decrypted (operation 1).
*/
// Function prototypes
void encrypt (int, char[]);
void decrypt (int, char[]);
char transform (char, int);
if(argc != 4)
{
cout<<"Expected an integer and a string "<<endl;
return 0;
}
/*
* Function name: transform
* Description: This function transforms (or shifts) <char> ch to
another character
* <int> key letters away in the standard alphabet.
* Parameters:
* ch - a single character in the alphabet
* key - an integer that holds the number of shifts
* Return value: returns a "transformed" char
*/
char transform(char ch, int key)
{
// Array alpha contains the entire alphabet
char alpha[] = "abcdefghijklmnopqrstuvwxyz";
// Handle the case where the key could be larger than the
number of characters in the alphabet
int new_key = key % 25;
/*
* Function name: encrypt
* Description: This function takes plain text <char> message and a
<int> key and executes
* the Caesar ciphter on the entire message using the transform
function.
* Parameters:
* key - the number of shifts to completed the cipher
* message - the message to be encrypted
* Return value: none
*/
void encrypt(int key, char mess[])
{int i=0;
while(mess[i]!='\0')
cout<<transform(mess[i++],key);
cout<<endl;
return;
}
/*
* Function name: decrypt
* Description: This function takes encrypted text <char> message and a
<int> key and executes
* the Caesar ciphter on the entire message using the transform
function.
* Parameters:
* key - the number of shifts to completed the cipher
* message - the message to be encrypted
* Return value: none
*/
while(mess[i]!='\0')
cout<<transform(mess[i++],-key);
cout<<endl;
return;
}