Week 07 Laboratory Exercises
Objectives
- processing of characters and strings
- use of functions
- an introduction to encryption & decryption
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Exercise — in pairs:
Devowelling Text
devowel.c
which reads characters from its input
and writes the same characters to its output, except it does not write lower case vowels ('a', 'e', 'i', 'o', 'u').
Your program should stop only at the end of input.
For example:
./devowel Are you saying 'Boo' or 'Boo-Urns'? Ar y syng 'B' r 'B-Urns'? In this house, we obey the laws of thermodynamics! In ths hs, w by th lws f thrmdynmcs!Hint: hint use
getchar
to read characters (don't use scanf
or fgets
).
Hint: you need only a single int variable. Don't use an array.
Hint: use putchar
to output each character.
Hint: make sure you understand this example program which reads characters until end of input.
Hint: make sure you understand this example program which reads characters, printing them with lower case letters converted to upper case.
Hint: create a function with a prototype like this:
int is_vowel(int character);which returns 1 the character is a lower case vowel and 0 otherwise.
Hint: To tell the program you have finished typing, you can press Ctrl+D.
1511 style devowel.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest devowel
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab07_devowel devowel.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Tuesday 06 April 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Its a Case of Swapping
Download swap_case.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/21T1/activities/swap_case/swap_case.c .
Edit the C program swap_case.c
(linked above) which reads characters from its input
and writes the same characters to its output with lower case letters converted to upper case
and upper case letters converted to lower case.
Your program should stop only at the end of input.
To help you format your code nicely, your program must use a function with signature:
int swap_case(int character);which:
- returns the character in lower case if it is an upper case letter
- returns the character in upper case if it is a lower case letter
- returns the character unchanged otherwise
Note: Your program will not pass autotests if it does not contain this function.
For example:
dcc swap_case.c -o swap_case ./swap_case Are you saying 'Boo' or 'Boo-Urns'? aRE YOU SAYING 'bOO' OR 'bOO-uRNS'? In this house, we obey the laws of thermodynamics! iN THIS HOUSE, WE OBEY THE LAWS OF THERMODYNAMICS! UPPER !@#$% lower upper !@#$% LOWERHint: hint use
getchar
to read characters (don't use scanf
or fgets
).
Hint: you need only a single int variable. Don't use an array.
Hint: use putchar
to output each character.
Hint: make sure you understand this example program which reads characters until end of input.
Hint: make sure you understand this example program which reads characters, printing them with lower case letters converted to upper case.
1511 style swap_case.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest swap_case
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab07_swap_case swap_case.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Tuesday 06 April 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Print out command line arguments in lower case
Write a C program, string_to_lower_args.c
, which reads command line arguments
then prints them out. When it prints out, it will convert all upper case letters to lower
case.
Note: If there are any characters that are not Upper Case letters, they do not need to be converted.
Note: The apostrophe ' character causes problems when trying to print it out. There will be no tests using an ' in this activity.
The output from your program should look exactly like this:
dcc string_to_lower_args.c -o string_to_lower_args ./string_to_lower_args Hello World! hello world! ./string_to_lower_args Its Over 9000! its over 9000! ./string_to_lower_args KAMEHAMEHA kamehameha
Need a Hint?
There's a way to decide if characters are upper case if they're between 'A' and 'Z'. They can then be treated as numbers to convert to lower case.
Otherwise, there is a C library called ctype.h that might have some useful functions!
1511 style string_to_lower_args.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest string_to_lower_args
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab07_string_to_lower_args string_to_lower_args.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Tuesday 06 April 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Encrypting Text with a Caesar Cipher
caesar.c
which reads characters from its input
and writes the characters to its output encrypted with a Caesar cipher.
A Caesar cipher shifts each letter a certain number of positions in the alphabet.
The number of positions to shift will be given to your program as a command line argument.
Characters other than letters should not be encrypted.
Your program should stop only at the end of input.
Your program should contain at least one function other than main.
For example:
./caesar 1 This life well it's slipping right through my hands Uijt mjgf xfmm ju't tmjqqjoh sjhiu uispvhi nz iboet These days turned out nothing like I had planned Uiftf ebzt uvsofe pvu opuijoh mjlf J ibe qmboofe ./caesar 10 abcdefghijklmnopqrstuvwxyz klmnopqrstuvwxyzabcdefghij ABCDEFGHIJKLMNOPQRSTUVWXYZ KLMNOPQRSTUVWXYZABCDEFGHIJ ./caesar -42 Control well it's slipping right through my hands Myxdbyv govv sd'c cvszzsxq bsqrd drbyeqr wi rkxnc These days? Droco nkic?Hint: handle upper and lower case letters separately
Hint: use %
Hint: use atoi
to convert the first command-line argument to an int.
Hint:make sure you understand this example program which uses a atoi
to convert command-line arguments to an ints.
Hint: create a function with a prototype like this:
int encrypt(int character, int shift);which returns the character shifted by the specified amount
Manually Cracking a Caesar Cipher
Here is some (New Zealand) English text that has been encrypted with a Caesar cipher.Z uf dp drbvlg ze jfdvsfup vcjv'j tri Nv fiuvi uzwwvivek uizebj rk kyv jrdv srij Z befn rsflk nyrk pfl uzu reu Z nreer jtivrd kyv kilky Jyv kyzebj pfl cfmv kyv svrty, pfl'iv jlty r urde czriUse the program you have just written to discover the secret text?
Hint:: try different shifts until you see English.
You program will only be tested with an appropriate command line argument - but a good programmer would check the command line argument is present and appropriate.
1511 style caesar.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest caesar
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab07_caesar caesar.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Tuesday 06 April 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Encrypting Text with a Substitution Cipher
A Substitution cipher maps each letter to another letter.
The mapping will be given to your program as a single command line argument. This command line argument will contain 26 characters: an ordering of the letters 'a'..'z'.
Characters other than letters should not be encrypted.
Your program should stop only at the end of input.
Your program should contain at least one function other than main.
For example:
./substitution qwertyuiopasdfghjklzxcvbnm I was scared of dentists and the dark O vql leqktr gy rtfzolzl qfr zit rqka I was scared of pretty girls and starting conversations O vql leqktr gy hktzzn uoksl qfr lzqkzofu egfctklqzogfl ./substitution abcdefghijklmnopqrstuvwxyz The identity cipher!!! The identity cipher!!! ./substitution bcdefghijklmnopqrstuvwxyza The Caesar cipher is a subset of the substitution cipher! Uif Dbftbs djqifs jt b tvctfu pg uif tvctujuvujpo djqifs!Your program will only be tested with an appropriate command line argument - but a good programmer would check the command line argument is present and appropriate.
1511 style substitution.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest substitution
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab07_substitution substitution.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Tuesday 06 April 20:00
to obtain the marks for this lab exercise.
Reflection #3
Don't forget that before your next lab you will need to complete Reflection #3
Find the instructions for the reflection here.
Follow the instructions here to copy the blog template and create a blog.
You will need to use the blog template to complete this exercise!
Challenge Exercise — individual:
Decrypting a Substitution Cipher
For example:
./decode qwertyuiopasdfghjklzxcvbnm O vql leqktr gy rtfzolzl qfr zit rqka I was scared of dentists and the dark O vql leqktr gy hktzzn uoksl qfr lzqkzofu egfctklqzogfl I was scared of pretty girls and starting conversations ./decode abcdefghijklmnopqrstuvwxyz The identity cipher!!! The identity cipher!!! ./decode bcdefghijklmnopqrstuvwxyza Uif Dbftbs djqifs jt b tvctfu pg uif tvctujuvujpo djqifs! The Caesar cipher is a subset of the substitution cipher!Your program will only be tested with an appropriate command line argument - but a good programmer would check the command line argument is present and appropriate.
Manually Cracking a Substitution Cipher
This English text was encrypted with a substitution cipher.Di jd, vdl'ht xtqa dh O qn Vdl rdlwk O'ss wdkith htqromu omkd ok O fhdwqwsv xdm'k Styk kd nv dxm rtzoetj Wlk kiqk'j kit royythtmet om dlh dfomodmj Vdl'ht q ndlkiyls Kiqk qndlmkj ydh qmdkith xtta dm nv dxm Mdx O'n q mdzts nqrt htjdlhetyls O jkqhk q eiqom xoki nv kidluik Kqsa oj eitqf, nv rqhsomu Xitm vdl'ht yttsomu houik qk idnt O xqmmq nqat vdl ndzt xoki edmyortmet O xqmmq wt xoki vdl qsdmtWhat was the original text?
Hint: use frequency_analysis.c on the encrypted text and compare the frequencies to English letter frequencies and then try your guesses with decode.c
1511 style decode.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest decode
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab07_decode decode.c
You must run give
before Tuesday 06 April 20:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
Challenge Exercise — individual:
Working Out the Letter Frequencies of Text
frequency_analysis.c
which reads characters from its input
until end of input.
It should then print the occurrence frequency for each of the 26 letters 'a'..'z'.
The frequency should be printed as a decimal value and an absolute number in exactly the format below.
Note upper and lower case letters are counted together.
For example:
./frequency_analysis Hello and goodbye. 'a' 0.066667 1 'b' 0.066667 1 'c' 0.000000 0 'd' 0.133333 2 'e' 0.133333 2 'f' 0.000000 0 'g' 0.066667 1 'h' 0.066667 1 'i' 0.000000 0 'j' 0.000000 0 'k' 0.000000 0 'l' 0.133333 2 'm' 0.000000 0 'n' 0.066667 1 'o' 0.200000 3 'p' 0.000000 0 'q' 0.000000 0 'r' 0.000000 0 's' 0.000000 0 't' 0.000000 0 'u' 0.000000 0 'v' 0.000000 0 'w' 0.000000 0 'x' 0.000000 0 'y' 0.066667 1 'z' 0.000000 0 ./frequency_analysis Hey! Hey! Hey! I don't like walking around this old and empty house So hold my hand, I'll walk with you my dear 'a' 0.072289 6 'b' 0.000000 0 'c' 0.000000 0 'd' 0.084337 7 'e' 0.084337 7 'f' 0.000000 0 'g' 0.012048 1 'h' 0.096386 8 'i' 0.072289 6 'j' 0.000000 0 'k' 0.036145 3 'l' 0.084337 7 'm' 0.036145 3 'n' 0.060241 5 'o' 0.084337 7 'p' 0.012048 1 'q' 0.000000 0 'r' 0.024096 2 's' 0.036145 3 't' 0.048193 4 'u' 0.036145 3 'v' 0.000000 0 'w' 0.036145 3 'x' 0.000000 0 'y' 0.084337 7 'z' 0.000000 0Hint: hint use
getchar
to read characters (don't use scanf
or fgets
).
Hint: make sure you understand this example program which reads characters until end of input.
Hint: use an array to store counts of each letter.
Hint: make sure you understand this example program which counts integers from the range 0..99.
Manually Cracking a Substitution Cipher
This English text was encrypted with a substitution cipher.Di jd, vdl'ht xtqa dh O qn Vdl rdlwk O'ss wdkith htqromu omkd ok O fhdwqwsv xdm'k Styk kd nv dxm rtzoetj Wlk kiqk'j kit royythtmet om dlh dfomodmj Vdl'ht q ndlkiyls Kiqk qndlmkj ydh qmdkith xtta dm nv dxm Mdx O'n q mdzts nqrt htjdlhetyls O jkqhk q eiqom xoki nv kidluik Kqsa oj eitqf, nv rqhsomu Xitm vdl'ht yttsomu houik qk idnt O xqmmq nqat vdl ndzt xoki edmyortmet O xqmmq wt xoki vdl qsdmtWhat was the original text?
Hint: use frequency_analysis.c on the encrypted text and compare the frequencies to English letter frequencies and then try your guesses with decode.c
1511 style frequency_analysis.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest frequency_analysis
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab07_frequency_analysis frequency_analysis.c
You must run give
before Tuesday 06 April 20:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
Extra-hard challenge: Cracking A Substitution Cipher (individual - attempt if you dare)
Your program should make no assumptions about the language of the original text - don't assume its English. In other words don't hard code English properties into your program, extract the statistical properties from the sample plain text. However, you can assume the English alphabet ('a'..'z').
Your program will be given as its first line of input the name of a file containing a large amount of unencrypted text in the same language as the encrypted text.
For example for example your program might be given this file containing 188k characters of English text (wikipedia sentences from here) Your program will be given the encrypted text on the next lines. You may read it all before printing the decryption.
For example:
./crack_substitution wiki_sentences.txt M'ka paat dra qegbu, ueta md xbb Rxu vw fxya teq Umxvetup, ogmbbmxtd, mt Oab-Xmg teq Red psvvag tmlrdp, vmu Jsbw Qrat wes xtu M qaga negakag qmbu Dra fgxzw uxwp, fmdw bmlrdp Dra qxw wes'u cbxw qmdr va bmya x frmbu Qmbb wes pdmbb beka va Qrat M'v te betlag westl xtu oaxsdmnsb? Qmbb wes pdmbb beka va Qrat M'ka led tedrmtl osd vw xfrmtl pesb? M yteq wes qmbb, M yteq wes qmbb M yteq drxd wes qmbb Qmbb wes pdmbb beka va qrat M'v te betlag oaxsdmnsb? M'ka paat dra qegbu, bmd md sc Xp vw pdxla teq Frxbbatlmtl xtlabp mt x taq xla teq Red psvvag uxwp, gefy t gebb Dra qxw wes cbxw neg va xd wesg preq Xtu xbb dra qxwp, M led de yteq Wesg cgaddw nxfa xtu abafdgmf pesb I've seen the world, done it all Had my cake now Diamonds, brilliant, in Bel-Air now Hot summer nights, mid July When you and I were forever wild The crazy days, city lights The way you'd play with me like a child Will you still love me When I'm no longer young and beautiful? Will you still love me When I've got nothing but my aching soul? I know you will, I know you will I know that you will Will you still love me when I'm no longer beautiful? I've seen the world, lit it up As my stage now Challenging angels in a new age now Hot summer days, rock n roll The way you play for me at your show And all the ways, I got to know Your pretty face and electric soulYou may assume the filename given on the first line of input is at most 1000 characters.
You may assume the encrypted text on stdin contains at most 10000 characters.
You may assume the unencrypted example text in the file contains at most 250000 characters.
Hint: you will need to look at the probabilities of sequences of 2 or perhaps 3 letters occurring or perhaps the probabilities of words.
Hint: use fopen
to open the file and fgetc
to read the file.
These won't be covered in lectures, so read this
example program to see how to use this functions to read a file.
An autotest is available to help you test your program but because this is a difficult problem it is possible very good attempts at the problem won't pass the autotests.
1511 style crack_substitution.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest crack_substitution
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab07_crack_substitution crack_substitution.c
You must run give
before Tuesday 06 April 20:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
Submission
give
.
You can run give
multiple times.
Only your last submission will be marked.
Don't submit any exercises you haven't attempted.
If you are working at home, you may find it more convenient to upload your work via give's web interface.
Remember you have until Week 8 Tuesday 20:00 to submit your work.
You cannot obtain marks by e-mailing your code to tutors or lecturers.
You check the files you have submitted here.
Automarking will be run by the lecturer several days after the submission deadline,
using test cases different to those autotest
runs for you.
(Hint: do your own testing as well as running autotest
.)
After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.
Lab Marks
When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:
1511 classrun -sturec