Programming Fundamentals

Information

  • This page contains additional revision exercises for week 07.
  • These exercises are not compulsory, nor do they provide any marks in the course.
  • You cannot submit any of these exercises, however autotests are available for them (Command included at bottom of each exercise).

Exercise — individual:
Secret Code

TOP SECRET // COMP1511 ONLY

Write a file secret_code.c which allows you to scan in messages encrypted with Tom's Secret Code, and then print them out (ending in a newline).

Tom's Secret Code works two letters at a time. Given some ciphertext (text that has been encrypted with Tom's Secret Code), take the first two letters. The first letter of the plaintext (unencrypted text) is the letter with the smaller ascii value of those two encrypted letters. For example, if the first two letters of ciphertext were "GD", the first letter of the plaintext would be "D".

To explain the code, the following diagram demonstrates how the code "CZuOMUPP1i5fg112" is decrypted as "COMP1511". In each pair of letters, the one with the lower ascii value was the one that was part of the plaintext.

Cipher Text C Z u O M U P P 1 i 5 f g 1 1 2
ASCII Values 67 90 117 79 77 85 80 80 49 105 53 102 103 49 49 50
Correct Answer C O M P 1 5 1 1

Your program should behave exactly as these examples do:

dcc secret_code.c -o secret_code
./secret_code
abbccddeeffggh
abcdefg 
./secret_code
CZuOMUPP1i5fg112
COMP1511 

Assumptions/Restrictions/Clarifications

  • You should not assume that there will be an even number of inputs. If there is an odd number of characters, you should ignore the last character.
  • You could be given any printable ascii character as input (lowercase letters, uppercase letters, newlines, symbols, etc.)
  • Your program should always print a newline at the end of it's output
  • This exercise is very difficult to solve using fgets. You should solve this using scanf("%c", ...)

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest secret_code