#include #include #include #include /* This macro gives the maximum key size. */ #define MAX_KEY_SIZE 10 /* This global variable stores the offsets given by the key */ int key[MAX_KEY_SIZE]; /* This global variable stores the size of the key in use */ int keysize = 0; /* This function, to be completed by the candidate, gets the key from the input and stores it in key and keysize The function should read characters from the input until a newline character, and turn those characters into a key as specified in the question. It should read but ignore any key characters after it has processed MAX_KEY_SIZE key letters. */ void ReadKey() { /* BEGIN SOLUTION (a) */ /* END SOLUTION (a) */ } /* This function, to be completed by the candidate, en/deciphers a single letter according to the key. Arguments: char c : the letter to be en/deciphered. Should be an uppercase letter. int decipher : true (1) if the letter is to be deciphered, false (0) if the letter is to be enciphered int count : the number of letters that have already been ciphered Return value: the ciphered letter. */ char cipher(char c, int decipher, int count) { /* check the input letter is valid */ if ( ! isalpha(c) || ! isupper(c) ) { fprintf(stderr,"cipher() called on non-(uppercase letter) %c\n",c); exit(-1); } /* BEGIN SOLUTION (b) */ /* END SOLUTION (b) */ } int main(int argc, char *argv[]) { int decipher = 0; int c; int count = 0; if ( argc > 1 && strcmp(argv[1],"-d") == 0 ) { decipher = 1; } ReadKey(); while ( (c = getchar()) != EOF ) { if ( isalpha(c) ) { putchar(cipher(toupper(c),decipher,count)); count++; } else { putchar(c); } } return EXIT_SUCCESS; }