Week 07 Laboratory Exercises

Objectives

  • processing of characters and strings
  • use of functions
  • an introduction to encryption & decryption

Activities To Be Completed

The following is a list of all the activities available to complete this week...

Worth one mark in total:

  • devowel
  • word_square
  • string_to_lower_args

Worth half a mark in total:

  • caesar
  • substitution

Worth half a mark in total:

  • decode
  • frequency_analysis

For your interest, but not for marks:

  • word_search

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

Exercise
(●◌◌)
:

Devowelling Text

Write a C program 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.

You can run an automated code style checker using the following command:
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 02 November 20:00 to obtain the marks for this lab exercise.

Exercise
(●◌◌)
:

Word Square

Write a program called word_square.c that prompts the user to enter a word, and afterwards, prints that word out n amount of times, where n is the length of the word.

./word_square
Input word: a

Word square is:
a
./word_square
Input word: word

Word square is:
word
word
word
word
./word_square
Input word: abrakadabra

Word square is:
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
You can assume that you will be given a word no longer than 1024 character (Including the null-terminator).
You can run an automated code style checker using the following command:
1511 style word_square.c

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

1511 autotest word_square

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab07_word_square word_square.c

Note, even though this is a pair exercise, you both must run give from your own account before Tuesday 02 November 20:00 to obtain the marks for this lab exercise.

Exercise
(●◌◌)
:

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!

You can run an automated code style checker using the following command:
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 02 November 20:00 to obtain the marks for this lab exercise.

Exercise
(●●◌)
:

Encrypting Text with a Caesar Cipher

Write a C program 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 czri
Use 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.

You can run an automated code style checker using the following command:
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 02 November 20:00 to obtain the marks for this lab exercise.

Exercise
(●●◌)
:

Encrypting Text with a Substitution Cipher

Write a C program substitution.c which reads characters from its input and writes the characters to its output encrypted 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.
You can run an automated code style checker using the following command:
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 02 November 20:00 to obtain the marks for this lab exercise.

Exercise
(●●●)
:

Decrypting a Substitution Cipher

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

./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 qsdmt
What 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

You can run an automated code style checker using the following command:
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 and your lab partner must both submit your work by running give:

give cs1511 lab07_decode decode.c

Note, even though this is a pair exercise, you both must run give from your own account before Tuesday 02 November 20:00 to obtain the marks for this lab exercise.

Exercise
(●●●)
:

Working Out the Letter Frequencies of Text

Write a C program 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 0
Hint: 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 qsdmt
What 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

You can run an automated code style checker using the following command:
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 and your lab partner must both submit your work by running give:

give cs1511 lab07_frequency_analysis frequency_analysis.c

Note, even though this is a pair exercise, you both must run give from your own account before Tuesday 02 November 20:00 to obtain the marks for this lab exercise.

Exercise
(☠)
:

Solving a 3D word search

Write a C program word_search.c which takes in:
  • Command line arguments: a number n followed by any number of words
  • Scanned input: An nxnxn cube of characters

3D Arrays

Note

3D arrays are outside the scope of this course. The use of them in this exercise is simply to allow those that are interested to use/explore them.

Reminder: This exercise is not worth any marks, so do not feel obliged to learn things that aren't in this course!

Before reading any further, it is important to note that you will be expected to use 3D arrays in this exercise (although it is possible without them). A small snippet on how to create a 3d array is shown below.

#include <stdio.h>

#define X_SIZE 5
#define Y_SIZE 3
#define Z_SIZE 7

int main(void) {
    // Declaring a 3D array of integers and initialising all elements to 0
    int cube[X_SIZE][Y_SIZE][Z_SIZE] = {0};

    // Filling element at index (2, 1, 5) with value '6'
    cube[2][1][5] = 6;

    // Printing this value
    printf("%d", cube[2][1][6]);
}

A 3D array can be thought of in a couple of ways

  • A Rectangular prism made up of small cubes (each array element)
  • An array of 2D arrays (array of grids)
  • An array of arrays of arrays (Rolls right off the tongue)
  • A CPU's nightmare

In this exercise, there is a cube which represents a 3D word search grid. This cube is filled in by from values scanned in when the program is actually run. Your goal is to determine whether each of the provided words in the command line arguments can be found within this cube.

The rules for finding a word are as follows:

  • Words can only appear in straight lines in the cube
  • Words will only be going through one dimension of the cube, this means if a word is found through the x dimension, all characters in that word will have the same y and z positions
  • Words cannot exist as diagonals
  • Words will not appear backwards. This means that words can only be found when going through each dimension. In terms of how this would work with a 3D array, you will never need to go backwards from an index when searching for a word
Here are some examples of what the programming is expected to do:
./word_search 2 on an po pi ai pa
What number would you like to see: 3
Please enter grid 0:
oa
np
Please enter grid 1:
on
ai

on found!
an found!
po not found!
pi found!
ai found!
pa not found!

./word_search 3 dog doge cat ood add friend app op man bad long dam supercalifragilisticexpialidocious low woo moo pod lip
Please enter grid 0:
daa
ood
gwd
Please enter grid 1:
opb
pda
pod
Please enter grid 2:
opo
edm
sup

dog found!
doge not found!
cat not found!
ood found!
add found!
friend not found!
app found!
op found!
man not found!
bad found!
long not found!
dam found!
supercalifragilisticexpialidocious not found!
low not found!
woo not found!
moo not found!
pod found!
lip not found!

Assumptions

  • You will always be given an integer as the first command line argument.
  • All inputs will be valid.
  • The cube size that is input will be in the range [1, 32] inclusive.
You can run an automated code style checker using the following command:
1511 style word_search.c

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

1511 autotest word_search

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab07_word_search word_search.c

Note, even though this is a pair exercise, you both must run give from your own account before Tuesday 02 November 20:00 to obtain the marks for this lab exercise.

Submission

When you are finished each exercises make sure you submit your work by running 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