COMP1721 - Higher Computing 1B

Higher Computing 1B - Week 4 Laboratory Exercises

Welcome to your third HigherComputing 1B laboratory.

This lab involves:

Create a separate directory (lab04 is a good name) using mkdir for this week's lab exercises.

Exercise 1: Printing Characters One Per Line

Write a C program, one.c, which reads in a string from the user and writes out the characters one per line.

For example:


% a.out
Enter string: Hello
H
e
l
l
o


Exercise 2: Palindrome

A palindrome is a sequence which is the same forwards as backwards.

Write a program, palindrome.c, which reads a string and tests if it is a palindrome.

For example:


% a.out
Enter possible palindrome: kayak
String is a palindrome
% a.out
Enter possible palindrome: canoe
String is not a palindrome


Design

The first stage is the construction of your program should be to prepare an the overall design. This lays out the structure of your program. This should be done in English (well any natural language - just not in C). For a program this size, a sketch on a single piece of paper is a suitable approach to design.

Exercise 3: Modifying Palindrome

Modify your Palindrome program so characters which are not letters are ignored and difference between upper case and lower case are ignored.

For example:


% a.out
Enter possible palindrome: Do geese see God? 
String is a palindrome
% a.out
Enter possible palindrome: Do ducks see God?
String is not a palindrome
% a.out
Enter possible palindrome: Madam, I'm Adam
String is a palindrome
% a.out
Enter possible palindrome: Madam, I'm Bruce
String is not a palindrome


Exercise 4: Letter Frequencies

In this lab exercise, you will complete a small C program, frequency.c, that counts the frequency of occurrence of letters of the alphabet in a piece of text.

Why might you want to do this? In the past, scholars of literature have actually used gross statistics of pieces of text, such as letter frequencies, to solve controversies over authorship.

The file data contains example input for your program. Save it in your directory. Its contents are:


This is a little file of text.
It's called "data".
It has 6 lines of input.

I'm going to use it for testing the
letter count program I've just written.

When given data as input this is how your program should behave.


% a.out
hello world
<control-d>
1 lines of input read.
10 alphabetic characters read.
2 non-alphabetic characters read.
The frequencies of the alphabetic characters was:
'd' 1
'e' 1
'h' 1
'l' 3
'o' 2
'r' 1
'w' 1
% a.out <data
6 lines of input read.
112 alphabetic characters read.
41 non-alphabetic characters read.
The frequencies of the alphabetic characters was:
'a' 6
'c' 2
'd' 2
'e' 12
'f' 4
'g' 4
'h' 3
'i' 14
'j' 1
'l' 7
'm' 2
'n' 6
'o' 7
'p' 2
'r' 5
's' 8
't' 20
'u' 4
'v' 1
'w' 1
'x' 1

This output tells us that, for example, the letter 't' occurs 20 times in the text while the letter 'z' doesn't occur anywhere.

You should quickly check that the program has done the right thing, by hand counting some of the characters above in the text and making sure that your count agrees with what the program produced.

There are a couple of things to observe about the way this program behaves. First, it treats 't' and 'T' as being equivalent; there is a 'T' in the data file, but it has been counted towards the overall count of 't's, if you check by hand.

Second, it counts in a separate category anything else that's not a letter of the alphabet, including digits.

Third, it doesn't print zero counts.

Finally, note very carefully the output format that the program uses; we want your program to produce its output in exactly the same format.

Hints

You will need an array to keep count of how many times you have seen each of the 26 letters.

Finished?

Make sure you test each program thoroughly.

When you are sure all are working submit them using give. Here is the command to use:

% /home/cs1721/bin/classrun -give lab04 one.c palindrome.c frequency.c

This will run some simple tests.

Then get your tutor to check your answers are correct and give you the lab mark for this week.



Andrew Taylor (andrewt@cse.unsw.edu.au)
Higher Computing 1B, Computer Science & Engineering, UNSW