You are on page 1of 7

02/04/2017 COMP1511 Week 06 Laboratory Exercises

COMP151117s1(http://www.cse.unsw.edu.au/~cs1511/17s1/)
Week06 Laboratory
IntroductiontoProgramming(http://www.cse.unsw.edu.au/~cs1511/17s1/)
Exercises

Objectives
InthisLab,youwillpractice:
simpleinputofstrings&characters
manipulatingstringsandcharacters

Preparation
Beforethelabyoushouldrereadtherelevantlectureslidesandtheiraccompanyingexamples.Youshouldalsohavereadthelab
assessmentguidelines(https://cgi.cse.unsw.edu.au/~cs1511cgi/lab_assessment.html).

GettingStarted
OnememberofyourprogrammingpairshouldloginandrunthefollowingcommandsinsideaLinuxterminal
Createanewdirectoryforthislabcalled lab06 bytyping:

$mkdirlab06

Changetothisdirectorybytyping:

$cdlab06

Exercise:Devowelling
WriteaCprogram devowel.c whichreadscharactersfromitsinputandwritesthesamecharacterstoitsoutput,exceptitdoes
notwritelowercasevowels('a','e','i','o','u').
Yourprogramshouldstoponlyattheendofinput.

Forexample:

$./devowel
Areyousaying'Boo'or'BooUrns'?
Arysyng'B'r'BUrns'?
Itwasthebestoftimes.Itwastheworstoftimes.
Itwsthbstftms.Itwsthwrstftms.
Ctrld
Hint:hintuse getchar toreadcharactersdon'tuse scanf or fgets .
Hint:use putchar tooutputeachcharacter.

Asusualautotestisavailabletotestyourprogram.

$~cs1511/bin/autotestlab06devowel.c

Exercise:CaesarCipher
WriteaCprogram caesar.c whichreadscharactersfromitsinputandwritesthecharacterstoitsoutputencryptedwitha
Caesarcipher(https://en.wikipedia.org/wiki/Caesar_cipher).
ACaesarcipher(https://en.wikipedia.org/wiki/Caesar_cipher)shiftseachletteracertainnumberofpositionsinthealphabet.

Thenumberofpositionstoshiftwillbegiventoyourprogramasacommandlineargument.

Charactersotherthanlettersshouldnotbeencrypted.

Yourprogramshouldstoponlyattheendofinput.

Yourprogramshouldcontainatleastonefunctionotherthanmain.

Forexample:

https://cgi.cse.unsw.edu.au/~cs1511/17s1/lab/06/questions 1/7
02/04/2017 COMP1511 Week 06 Laboratory Exercises

$./caesar1
Thislifewellit'sslippingrightthroughmyhands
Uijtmjgfxfmmju'ttmjqqjohsjhiuuispvhinziboet
ThesedaysturnedoutnothinglikeIhadplanned
UiftfebztuvsofepvuopuijohmjlfJibeqmboofe
Ctrld
$./caesar10
abcdefghijklmnopqrstuvwxyz
klmnopqrstuvwxyzabcdefghij
ABCDEFGHIJKLMNOPQRSTUVWXYZ
KLMNOPQRSTUVWXYZABCDEFGHIJ
Ctrld
$./caesar42
Controlwellit'sslippingrightthroughmyhands
Myxdbyvgovvsd'ccvszzsxqbsqrddrbyeqrwirkxnc
Thesedays?
Droconkic?
Ctrld
Hint:handleupperandlowercaselettersseparately
Hint:use %

Hint:use atoi toconvertthecommandlineargumenttoanint.

Asusualautotestisavailabletotestyourprogram.

$~cs1511/bin/autotestlab06caesar.c

Exercise:ManuallyCrackingaCaesarCipher
ThistextwasencryptedwithaCaesarcipher.

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

Whatistheplaintext?(hint:tryvariousshiftswithcaesar.c)

Exercise:LetterFrequency
WriteaCprogram letter_frequency.c whichreadscharactersfromitsinputuntilendofinput.
Itshouldthenprinttheoccurancefrequencyforeachofthe26letters'a'..'z'.

Thefrequencyshouldbeprintedasadecimalvalueandanabsolutenumberinexactlytheformatbelow.

Noteupperandlowercaselettersarecountedtogether.

Forexample:

https://cgi.cse.unsw.edu.au/~cs1511/17s1/lab/06/questions 2/7
02/04/2017 COMP1511 Week 06 Laboratory Exercises

$./letter_frequency
Helloandgoodbye.
Ctrld
'a'0.0666671
'b'0.0666671
'c'0.0000000
'd'0.1333332
'e'0.1333332
'f'0.0000000
'g'0.0666671
'h'0.0666671
'i'0.0000000
'j'0.0000000
'k'0.0000000
'l'0.1333332
'm'0.0000000
'n'0.0666671
'o'0.2000003
'p'0.0000000
'q'0.0000000
'r'0.0000000
's'0.0000000
't'0.0000000
'u'0.0000000
'v'0.0000000
'w'0.0000000
'x'0.0000000
'y'0.0666671
'z'0.0000000
$./letter_frequency
Hey!Hey!Hey!
Idon'tlikewalkingaroundthisoldandemptyhouse
Soholdmyhand,I'llwalkwithyoumydear
Ctrld
'a'0.0722896
'b'0.0000000
'c'0.0000000
'd'0.0843377
'e'0.0843377
'f'0.0000000
'g'0.0120481
'h'0.0963868
'i'0.0722896
'j'0.0000000
'k'0.0361453
'l'0.0843377
'm'0.0361453
'n'0.0602415
'o'0.0843377
'p'0.0120481
'q'0.0000000
'r'0.0240962
's'0.0361453
't'0.0481934
'u'0.0361453
'v'0.0000000
'w'0.0361453
'x'0.0000000

https://cgi.cse.unsw.edu.au/~cs1511/17s1/lab/06/questions 3/7
02/04/2017 COMP1511 Week 06 Laboratory Exercises
'y'0.0843377
'z'0.0000000
Hint:useanarrayofcounts.
Asusualautotestisavailabletotestyourprogram.

$~cs1511/bin/autotestlab06letter_frequency.c

Exercise:SubstitutionCipherEncoding
WriteaCprogramsubstitution.cwhichreadscharactersfromitsinputandwritesthecharacterstoitsoutputencryptedwitha
Substitutioncipher(https://en.wikipedia.org/wiki/Substitution_cipher).
ASubstitutioncipher(https://en.wikipedia.org/wiki/Substitution_cipher)mapseachlettertoanotherletter.

Themappingwillbegiventoyourprogramasasinglecommandlineargument.Thiscommandlineargumentwillcontain26
characters:anorderingoftheletters'a'..'z'.

Charactersotherthanlettersshouldnotbeencrypted.

Yourprogramshouldstoponlyattheendofinput.

Yourprogramshouldcontainatleastonefunctionotherthanmain.

Forexample:

$./substitutionqwertyuiopasdfghjklzxcvbnm
Iwasscaredofdentistsandthedark
Ovqlleqktrgyrtfzolzlqfrzitrqka
Iwasscaredofprettygirlsandstartingconversations
Ovqlleqktrgyhktzznuokslqfrlzqkzofuegfctklqzogfl
Ctrld
$./substitutionabcdefghijklmnopqrstuvwxyz
Theidentitycipher!!!
Theidentitycipher!!!
$./substitutionbcdefghijklmnopqrstuvwxyza
TheCaesarcipherisasubsetofthesubstitutioncipher!
UifDbftbsdjqifsjtbtvctfupguiftvctujuvujpodjqifs!
Ctrld
Asusualautotestisavailabletotestyourprogram.

$~cs1511/bin/autotestlab06substitution.c

Exercise:SubstitutionCipherDecoding
WriteaCprogramdecode.cwhichdecryptstextencryptedbysubstitution.c
Forexample:

$./decodeqwertyuiopasdfghjklzxcvbnm
Ovqlleqktrgyrtfzolzlqfrzitrqka
Iwasscaredofdentistsandthedark
Ovqlleqktrgyhktzznuokslqfrlzqkzofuegfctklqzogfl
Iwasscaredofprettygirlsandstartingconversations
Ctrld
$./decodeabcdefghijklmnopqrstuvwxyz
Theidentitycipher!!!
Theidentitycipher!!!
$./substitutionbcdefghijklmnopqrstuvwxyza
UifDbftbsdjqifsjtbtvctfupguiftvctujuvujpodjqifs!
TheCaesarcipherisasubsetofthesubstitutioncipher!
Ctrld
Asusualautotestisavailabletotestyourprogram.

$~cs1511/bin/autotestlab06decode.c

Exercise:ManuallyCrackingaSubstitutionCipher
https://cgi.cse.unsw.edu.au/~cs1511/17s1/lab/06/questions 4/7
02/04/2017 COMP1511 Week 06 Laboratory Exercises
ThisEnglishtextwasencryptedwithaSubstitutioncipher.

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

Whatwastheoriginaltext?
Hint:useletter_frequency.contheencryptedtextandcomparethefrequenciestoEnglishletterfrequencies
(https://en.wikipedia.org/wiki/Letter_frequency)andthentryyourguesseswithdecode.c

ChallengeExercise:AutomaticallyCrackingCaesarCiphers
WriteaCprogramcrack_caesar.cwhichdecryptstextencryptedbyanunknownCaesarcipher.
Yourprogramshouldmakenoassumptionsaboutthelanguageoftheoriginaltextdon'tassumeitsEnglish.

Yourprogramwillbegivenasacommandlineargumentthenameofafilecontainingalargeamountofunencryptedtextinthe
samelanguageastheencryptedtext.

Yourprogramwillbegiventheencryptedtextonstandardinput.Youmayreaditallbeforeprintingthedecryption.

Youmayassumetheencryptedtextcontainsatmost10000characters.Forexample:
/web/cs1511cgi/17s1/tlb/06unsw.txt contains42000charactersofEnglishtext(UNSWstudentsonfacebook)
Forexample,hereissomeEnglishtextencryptedwithaCaesarcipherwithanunknownshift:

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

Soforexample:

$./crack_caesar/web/cs1511cgi/17s1/tlb/06unsw.txt
Kyzjzjflicrjkxffuspv
Zyrkvkfwvvckyvcfmvsvknvveljuzv
Slkzk'jfmvi
AljkyvrikyzjreukyveZ'ccxf
Pflxrmvdvdfivkfczmvwfi
Dfivkyrepfl'ccvmvibefn
Ctrld
Thisisourlastgoodbye
Ihatetofeelthelovebetweenusdie
Butit'sover
JusthearthisandthenI'llgo
Yougavememoretolivefor
Morethanyou'lleverknow
Hint:extracttheletterfrequenciesfromthefileofunencryptedtext.Usethesefrequenciesasestimateoftheprobabilityeach
letterwillbeused.Testallpossibleshiftsusingtheseprobabilitiestoseewhichisthemostlikely..
Asusualautotestisavailabletotestyourprogram.

$~cs1511/bin/autotestlab06crack_caesar.c

ChallengeExercise:AutomaticallyCrackingSubstitutionCiphers
WriteaCprogramcrack_substitution.cwhichdecryptstextencryptedbyanunknownscipher.
Yourprogramshouldmakenoassumptionsaboutthelanguageoftheoriginaltextdon'tassumeitsEnglish.

Yourprogramwillbegivenasacommandlineargumentthenameofafilecontainingalargeamountofunencryptedtextinthe
samelanguageastheencryptedtext.

Yourprogramwillbegiventheencryptedtextonstandardinput.Youmayreaditallbeforeprintingthedecryption.

Youmayassumetheencryptedtextcontainsatmost10000characters.

Forexample:

https://cgi.cse.unsw.edu.au/~cs1511/17s1/lab/06/questions 5/7
02/04/2017 COMP1511 Week 06 Laboratory Exercises

$./crack_substitution/web/cs1511cgi/17s1/tlb/06unsw.txt
M'kapaatdraqegbu,uetamdxbb
Rxuvwfxyateq
Umxvetup,ogmbbmxtd,mtOabXmgteq
Redpsvvagtmlrdp,vmuJsbw
QratwesxtuMqaganegakagqmbu
Drafgxzwuxwp,fmdwbmlrdp
Draqxwwes'ucbxwqmdrvabmyaxfrmbu
Qmbbwespdmbbbekava
QratM'vtebetlagwestlxtuoaxsdmnsb?
Qmbbwespdmbbbekava
QratM'kaledtedrmtlosdvwxfrmtlpesb?
Myteqwesqmbb,Myteqwesqmbb
Myteqdrxdwesqmbb
QmbbwespdmbbbekavaqratM'vtebetlagoaxsdmnsb?
M'kapaatdraqegbu,bmdmdsc
Xpvwpdxlateq
Frxbbatlmtlxtlabpmtxtaqxlateq
Redpsvvaguxwp,gefytgebb
Draqxwwescbxwnegvaxdwesgpreq
Xtuxbbdraqxwp,Mleddeyteq
Wesgcgaddwnxfaxtuabafdgmfpesb
Ctrld
I'veseentheworld,doneitall
Hadmycakenow
Diamonds,brilliant,inBelAirnow
Hotsummernights,midJuly
WhenyouandIwereforeverwild
Thecrazydays,citylights
Thewayyou'dplaywithmelikeachild
Willyoustillloveme
WhenI'mnolongeryoungandbeautiful?
Willyoustillloveme
WhenI'vegotnothingbutmyachingsoul?
Iknowyouwill,Iknowyouwill
Iknowthatyouwill
WillyoustilllovemewhenI'mnolongerbeautiful?
I'veseentheworld,lititup
Asmystagenow
Challengingangelsinanewagenow
Hotsummerdays,rocknroll
Thewayyouplayformeatyourshow
Andalltheways,Igottoknow
Yourprettyfaceandelectricsoul
Hint:youwillneedtolookattheprobabilitiesofsequencesof2orperhaps3lettersoccurringortheprobabilitiesofwords.
Anautotestisavailabletotestyourprogrambutbecausethisisadifficultproblemitispossibleverygoodattemptsattheproblem
won'tpasstheautotests.

$~cs1511/bin/autotestlab06crack_caesar.c

Submission/Assessment
Whenyouaresatisfiedwithyourwork,askyourtutortoassessit.Youalsoneedtosubmityourworkelectronicallybytyping(run
thiscommandinthe lab06 directory):

$givecs1511lab06devowel.ccaesar.cletter_frequency.c

Submitthechallengeexercisesonlyifyouattemptthem.
Ifyouareworkingathome,youmayfinditmoreconvenienttouploadyourworkviagive'swebinterface
(https://cgi.cse.unsw.edu.au/~give/code/login.php?app=/~give/Student/give.php&debug=get_authuser).

https://cgi.cse.unsw.edu.au/~cs1511/17s1/lab/06/questions 6/7
02/04/2017 COMP1511 Week 06 Laboratory Exercises
Rememberthelabassessmentguidelines(https://cgi.cse.unsw.edu.au/~cs1511cgi/lab_assessment.html)ifyoudon'tfinishthe
exercisesyoucanfinishtheminyourowntime,submitthembyMonday11:00amusing give andaskyourtutortoassessthem
atthestartofthefollowinglab.

Eitherorbothmembersofaprogrammingpaircansubmitthework(makesureeachprogramlistsbothofyouasauthorsinthe
headercomment).

https://cgi.cse.unsw.edu.au/~cs1511/17s1/lab/06/questions 7/7