COMP1511
COMP1511 17s1 17s1 (http://www(http://www.cse.unsw.edu.au/~cs151.cse.unsw.edu.au/~cs1511/17s1/)1/17s1/)
Introduction to Programming (http://www.cse.unsw.edu.au/~cs1511/17s1/) Introduction to Programming (http://www.cse.unsw.edu.au/~cs1511/17s1/)
Objectives
Objectives
In
In this Lab, you will practice: this Lab, you will practice:
simple input of strings & characters simple input of strings & characters manipulating strings and characters manipulating strings and characters
Preparation
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples. You should also have read the lab Before the lab you should re-read the relevant lecture slides and their accompanying examples. You should also have read the lab assessment guidelines
assessment guidelines (https://cgi.cse.unsw.edu.au/~cs1511cgi/l(https://cgi.cse.unsw.edu.au/~cs1511cgi/lab_assessment.html).ab_assessment.html).
Getting Started
Getting Started
One member of your programming pair should login and run the following commands inside a Linux terminal One member of your programming pair should login and run the following commands inside a Linux terminal Create a new directory for this lab called
Create a new directory for this lab called
lab06
lab06
by typing: by typing: $$
mkdir lab06
mkdir lab06
Change to this directory by typing: Change to this directory by typing:
$
$
cd lab06
cd lab06
Exercise: Devowelling
Exercise: Devowelling
Write a C program
Write a C program
devowel.c
devowel.c
which reads characters from its input and writes the same characters to its output, except it does 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').not write lower case vowels ('a', 'e','i', 'o', 'u'). Your program should stop only at the end of input. Your program should stop only at the end of input. For example:
For example: $
$
./devowel
./devowel
Are you saying 'Boo' or 'Boo-Urns'?
Are you saying 'Boo' or 'Boo-Urns'?
Ar y syng 'B' r 'B-Urns'?
Ar y syng 'B' r 'B-Urns'?
It was the best of times. It was the worst of times.
It was the best of times. It was the worst of times.
It ws th bst f tms. It ws th wrst f tms.
It ws th bst f tms. It ws th wrst f tms.
Ctrl-d Ctrl-d Hint
Hint: hint use: hint use
getchar
getchar
to read characters don't useto read characters don't usescanf
scanf
ororfgets
fgets
.. HintHint: use: use
putchar
putchar
to output each character. to output each character. As usual autotest is available to test your program. As usual autotest is available to test your program.$
$
~cs1511/bin/autotest lab06 devowel.c
~cs1511/bin/autotest lab06 devowel.c
Exercise: Caesar Cipher
Exercise: Caesar Cipher
Write a C program
Write a C program
caesar.c
caesar.c
which reads characters from its input and writes the characters to its output encrypted with a which reads characters from its input and writes the characters to its output encrypted with a Caesar cipher (https://en.wikipedia.org/wiki/Caesar_cipher).Caesar cipher (https://en.wikipedia.org/wiki/Caesar_cipher).
A Caesar cipher (https://en.wikipedia.org/wiki/Caesar_cipher) shifts each letter a certain number of positions in the alphabet. A Caesar cipher (https://en.wikipedia.org/wiki/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.
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.
Characters other than letters should not be encrypted. Your program should stop only at the end of input. Your program should stop only at the end of input.
Your program should contain at least one function other than main. Your program should contain at least one function other than main. For example:
For example:
Week
Week 06
06
Laboratory
Laboratory
Exercises
$
./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
Ctrl-d $./caesar 10
abcdefghijklmnopqrstuvwxyz
klmnopqrstuvwxyzabcdefghij
ABCDEFGHIJKLMNOPQRSTUVWXYZ
KLMNOPQRSTUVWXYZABCDEFGHIJ
Ctrl-d $./caesar -42
Control well it's slipping right through my hands
Myxdbyv govv sd'c cvszzsxq bsqrd drbyeqr wi rkxnc
These days?
Droco nkic?
Ctrl-dHint: handle upper and lower case letters separately Hint: use
%
Hint: use
atoi
to convert the command-line argument to an int. As usual autotest is available to test your program.$
~cs1511/bin/autotest lab06 caesar.c
Exercise: Manually Cracking a Caesar Cipher
This text was encrypted with a Caesarcipher. Naq jr'yy arire or eblnyf
Vg qba'g eha va bhe oybbq
Gung xvaq bs yhk whfg nva'g sbe hf Jr penir n qvssrerag xvaq bs ohmm
What is the plain-text? (hint: try various shifts with caesar.c)
Exercise: Letter Frequency
Write a C program
letter_frequency.c
which reads characters from its input until end of input. It should then print the occurance 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.
$
./letter_frequency
Hello and goodbye.
Ctrl-d
'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
$./letter_frequency
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
Ctrl-d
'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 0
Hint: use an array of counts.As usual autotest is available to test your program.
$
~cs1511/bin/autotest lab06 letter_frequency.c
Exercise: Substitution Cipher Encoding
Write a C program substitution.c which reads characters from its input and writes the characters to its output encrypted with a Substitution cipher (https://en.wikipedia.org/wiki/Substitution_cipher).
A Substitution cipher (https://en.wikipedia.org/wiki/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
Ctrl-d$
./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!
Ctrl-dAs usual autotest is available to test your program. $
~cs1511/bin/autotest lab06 substitution.c
Exercise: Substitution Cipher Decoding
Write a C program decode.c which decrypts text encrypted by substitution.c 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
Ctrl-d$
./decode abcdefghijklmnopqrstuvwxyz
The identity cipher!!!
The identity cipher!!!
$
./substitution bcdefghijklmnopqrstuvwxyza
Uif Dbftbs djqifs jt b tvctfu pg uif tvctujuvujpo djqifs!
The Caesar cipher is a subset of the substitution cipher!
Ctrl-dAs usual autotest is available to test your program. $
~cs1511/bin/autotest lab06 decode.c
This English text was encrypted with a Substitution cipher. Ndwu fdw sojfauw xafwl dvsq
Vuq vcxafaoul vsw yon
Vuq fdw swlwufcwuf saqwl datd Xjf wcofaoul nouf tson
Vuq nwsw kdvutaut ojs nvrl, Fviaut qappwswuf sovql
Fdwu yomw, yomw nayy fwvs jl vbvsf vtvau What was the original text?
Hint: use letter_frequency.c on the encrypted text and compare the frequencies to English letter frequencies (https://en.wikipedia.org/wiki/Letter_frequency) and then try your guesses with decode.c
Challenge Exercise: Automatically Cracking Caesar Ciphers
Write a C program crack_caesar.c which decrypts text encrypted by an unknown Caesar cipher.
Your program should make no assumptions about the language of the original text - don't assume its English.
Your program will be given as a command-line argument the name of a file containing a large amount of unencrypted text in the same language as the encrypted text.
Your program will be given the encrypted text on standard input. You may read it all before printing the decryption. You may assume the encrypted text contains at most 10000 characters. For example:
/web/cs1511cgi/17s1/tlb/06unsw.txt
contains 42000 characters of English text (UNSW students on facebook) For example, here is some English text encrypted with a Caesar cipher with an unknown shift:Kyzj zj fli crjk xffuspv
Z yrkv kf wvvc kyv cfmv svknvve lj uzv Slk zk'j fmvi
Aljk yvri kyzj reu kyve Z'cc xf Pfl xrmv dv dfiv kf czmv wfi Dfiv kyre pfl'cc vmvi befn So for example:
$
./crack_caesar /web/cs1511cgi/17s1/tlb/06unsw.txt
Kyzj zj fli crjk xffuspv
Z yrkv kf wvvc kyv cfmv svknvve lj uzv
Slk zk'j fmvi
Aljk yvri kyzj reu kyve Z'cc xf
Pfl xrmv dv dfiv kf czmv wfi
Dfiv kyre pfl'cc vmvi befn
Ctrl-d
This is our last goodbye
I hate to feel the love between us die
But it's over
Just hear this and then I'll go
You gave me more to live for
More than you'll ever know
Hint: extract the letter frequencies from the file of unencrypted text. Use these frequencies as estimate of the probability each letter will be used. Test all possible shifts using these probabilities to see which is the most likely..
As usual autotest is available to test your program. $
~cs1511/bin/autotest lab06 crack_caesar.c
Challenge Exercise: Automatically Cracking Substitution Ciphers
Write a C program crack_substitution.c which decrypts text encrypted by an unknown s cipher.
Your program should make no assumptions about the language of the original text - don't assume its English.
Your program will be given as a command-line argument the name of a file containing a large amount of unencrypted text in the same language as the encrypted text.
Your program will be given the encrypted text on standard input. You may read it all before printing the decryption. You may assume the encrypted text contains at most 10000 characters.
$
./crack_substitution /web/cs1511cgi/17s1/tlb/06unsw.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
Ctrl-d
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 soul
Hint: you will need to look at the probabilities of sequences of 2 or perhaps 3 letters occurring or the probabilities of words. An autotest is available to test your program but because this is a difficult problem it is possible very good attempts at the problem won't pass the autotests.
$
~cs1511/bin/autotest lab06 crack_caesar.c
Submission/Assessment
When you are satisfied with your work, ask your tutor to assess it. You also need to submit your work electronically by typing (run this command in the
lab06
directory):$
give cs1511 lab06 devowel.c caesar.c letter_frequency.c
Submit the challenge exercises only if you attempt them.
Remember the lab assessment guidelines (https://cgi.cse.unsw.edu.au/~cs1511cgi/lab_assessment.html) - if you don't finish the exercises you can finish them in your own time, submit them by Monday 11:00am using
give
and ask your tutor to assess them at the start of the following lab.Either or both members of a programming pair can submit the work (make sure each program lists both of you as authors in the header comment).