CS1101C Lab 9

The deadline for this lab is Friday 7 November 2008, 23:59:59 hours.

Preliminary — Pangrams

A pangram is a sentence that uses every letter of the alphabet at least once. The most famous example of a pangram is

The quick brown fox jumps over the lazy dog.

In the questions below, you shall be working with a list of pangrams.

Question 1 — Reading pangrams

A list of pangrams is given to you in the file pangrams.txt. Each pangram consists only of lower case letters and blank spaces. There are no punctuation marks. The longest pangram in the file is 58 characters long.

The program lab9.c is given to you that reads individual pangrams from the file and displays it. The program uses the fgets function to read individual lines from the file, and uses the feof function to check if the preceding fgets encountered the end of the file. Below is a description of how these two functions are used within the program.

fgets(str, n, infile)
The fgets() function reads characters from the stream infile into the character array str, until n-1 characters are read, or a newline character '\n' is read and transferred to str, or an end-of-file condition is encountered. The string is then terminated with a null character '\0'. If end-of-file is encountered, the EOF indicator for the stream infile is set.

feof(infile)
The feof() function returns a non-zero value when EOF has previously been detected reading the stream infile. It returns 0 otherwise.

Your task is to extend the main function and store all the pangrams into an array of strings. You can assume that the number of pangrams will not exceed 40.

In your program,

  1. Define a constant NUMLINES and set it to 40.
  2. Declare an appropriate array in the main function to store the list of pangrams to be read in from the file.
    Hint: Since a string is an array of characters, so an array of strings is...
  3. Notice from the print out of lab9.c that each pangram read from the file includes the newline '\n' character. Remove this newline character.
    Hint: In which position is the newline character of a string? Removing this newline can be achieved with one statement using the strlen method of the string library and the '\0' character.
  4. Store each pangram into the array of strings declared in step 2.
  5. Include a helper function printList in your program to display the list of pangrams. The printList function should take in an array of strings and the number of strings to be displayed.

The sample output when printList is invoked for pangrams.txt is as follows:

junk mtv quiz graced by fox whelps
quick wafting zephyrs vex bold jim
sex charged fop blew my junk tv quiz
how quickly daft jumping zebras vex
two driven jocks help fax my big quiz
vamp fox held quartz duck just by wing
five quacking zephyrs jolt my wax bed
the five boxing wizards jump quickly
jackdaws love my big sphinx of quartz
cozy sphinx waves quart jug of bad milk
a very bad quack might jinx zippy fowls
pack my box with five dozen liquor jugs
few quips galvanized the mock jury box
blowzy red vixens fight for a quick jump
a quick brown fox jumps over the lazy dog
joaquin phoenix was gazed by mtv for luck
the quick brown fox jumps over the lazy dog
heavy boxes perform quick waltzes and jigs
a wizards job is to vex chumps quickly in fog
woven silk pyjamas exchanged for blue quartz
brawny gods just flocked up to quiz and vex him
my faxed joke won a pager in the cable tv quiz show
the quick onyx goblin jumps over the lazy dwarf
amazingly few discotheques provide jukeboxes
cozy lummox gives smart squid who asks for job pen
pack my box with five dozen jugs of liquid veneer
my girl wove six dozen plaid jackets before she quit
wolf zombies quickly spot the jinxed grave
six big devils from japan quickly forgot how to waltz
big july earthquakes confound zany experimental vow
foxy parsons quiz and cajole the lovably dim wiki girl
six boys guzzled cheap n raw plum vodka quite joyfully
jack quietly moved up front and seized the big ball of wax
few black taxis drive up major roads on quiet hazy nights
crazy fredericka bought many very exquisite opal jewels
sixty zippers were quickly picked from the woven jute bag
how razorback jumping frogs can level six piqued gymnasts
a quick movement of the enemy will jeopardize six gunboats
all questions asked by five watch experts amazed the judge
painful zombies quickly watch a jinxed graveyard

The name of your C program file must be called lab9q1.c, files with any other name will not be marked.

Question 2 — Sorting pangrams

We would like to sort our pangrams in lexicographical order according to the ASCII character set. By using the selection sort algorithm to sort the pangrams, write the function sortList that takes in the array of strings and the number of strings to be sorted. Use the strcmp and strcpy methods of the string library to achieve your task.

Within the main function, invoke the sortList function followed by the printList function. The sample output is given below.

a quick brown fox jumps over the lazy dog
a quick movement of the enemy will jeopardize six gunboats
a very bad quack might jinx zippy fowls
a wizards job is to vex chumps quickly in fog
all questions asked by five watch experts amazed the judge
amazingly few discotheques provide jukeboxes
big july earthquakes confound zany experimental vow
blowzy red vixens fight for a quick jump
brawny gods just flocked up to quiz and vex him
cozy lummox gives smart squid who asks for job pen
cozy sphinx waves quart jug of bad milk
crazy fredericka bought many very exquisite opal jewels
few black taxis drive up major roads on quiet hazy nights
few quips galvanized the mock jury box
five quacking zephyrs jolt my wax bed
foxy parsons quiz and cajole the lovably dim wiki girl
heavy boxes perform quick waltzes and jigs
how quickly daft jumping zebras vex
how razorback jumping frogs can level six piqued gymnasts
jack quietly moved up front and seized the big ball of wax
jackdaws love my big sphinx of quartz
joaquin phoenix was gazed by mtv for luck
junk mtv quiz graced by fox whelps
my faxed joke won a pager in the cable tv quiz show
my girl wove six dozen plaid jackets before she quit
pack my box with five dozen jugs of liquid veneer
pack my box with five dozen liquor jugs
painful zombies quickly watch a jinxed graveyard
quick wafting zephyrs vex bold jim
sex charged fop blew my junk tv quiz
six big devils from japan quickly forgot how to waltz
six boys guzzled cheap n raw plum vodka quite joyfully
sixty zippers were quickly picked from the woven jute bag
the five boxing wizards jump quickly
the quick brown fox jumps over the lazy dog
the quick onyx goblin jumps over the lazy dwarf
two driven jocks help fax my big quiz
vamp fox held quartz duck just by wing
wolf zombies quickly spot the jinxed grave
woven silk pyjamas exchanged for blue quartz

The name of your C program file must be called lab9q2.c, files with any other name will not be marked.

Question 3 — Sorting pangrams again

Using the same selection sort algorithm, we would now like to sort our pangrams in order of the largest number of 'z's. If there are two pangrams with the same number of 'z's, then arrange them in order of the largest number of 'y's. If there are two pangrams with the same number of 'z's and 'y's, then arrange them in order of the largest number of 'x's. The process of ordering continues with all alphabets down to 'a'. We shall assume that no two pangrams have the same number of individual alphabets. The sample output is as follows.

six boys guzzled cheap n raw plum vodka quite joyfully
crazy fredericka bought many very exquisite opal jewels
sixty zippers were quickly picked from the woven jute bag
big july earthquakes confound zany experimental vow
the quick onyx goblin jumps over the lazy dwarf
painful zombies quickly watch a jinxed graveyard
foxy parsons quiz and cajole the lovably dim wiki girl
a very bad quack might jinx zippy fowls
five quacking zephyrs jolt my wax bed
six big devils from japan quickly forgot how to waltz
my faxed joke won a pager in the cable tv quiz show
pack my box with five dozen jugs of liquid veneer
brawny gods just flocked up to quiz and vex him
vamp fox held quartz duck just by wing
all questions asked by five watch experts amazed the judge
few black taxis drive up major roads on quiet hazy nights
a quick movement of the enemy will jeopardize six gunboats
jack quietly moved up front and seized the big ball of wax
the quick brown fox jumps over the lazy dog
cozy lummox gives smart squid who asks for job pen
how razorback jumping frogs can level six piqued gymnasts
a wizards job is to vex chumps quickly in fog
amazingly few discotheques provide jukeboxes
woven silk pyjamas exchanged for blue quartz
cozy sphinx waves quart jug of bad milk
a quick brown fox jumps over the lazy dog
blowzy red vixens fight for a quick jump
joaquin phoenix was gazed by mtv for luck
pack my box with five dozen liquor jugs
few quips galvanized the mock jury box
the five boxing wizards jump quickly
how quickly daft jumping zebras vex
sex charged fop blew my junk tv quiz
junk mtv quiz graced by fox whelps
my girl wove six dozen plaid jackets before she quit
wolf zombies quickly spot the jinxed grave
heavy boxes perform quick waltzes and jigs
jackdaws love my big sphinx of quartz
two driven jocks help fax my big quiz
quick wafting zephyrs vex bold jim

You are free to implement the program in anyway you wish. Principles of modularity should still be obeyed though. The name of your C program file must be called lab9q3.c, files with any other name will not be marked.

Question 4 — Developing your own string functions

In question 2, you would have used the string functions available from the string library. Being proficient programmers now, your task is to re-implement the string functions yourselves. Write equivalent mystrlen, mystrcmp and mystrcpy functions to replace the string functions you have used.

Use the following function prototypes for your programmer defined functions and implement the functions according to the descriptions provided.

int mystrlen(char s[]);
The mystrlen() function returns the number of characters in s, not including the terminating null character.

int mystrcmp(char s1[], char s2[]);
The mystrcmp() function compares two strings character-by-character, according to the ordering of the ASCII character set. The function returns an integer greater than, equal to, or less than 0, if the string s1 is greater than, equal to, or less than the string s2 respectively. The sign of a non-zero return value is determined by the sign of the difference between the values of the first pair of characters that differ in the strings being compared.

void mystrcpy(char s1[], char s2[]);
The mystrcpy() function copies string s2 to s1, including the terminating null character, stopping after the null character has been copied.

In your program, you should not use any of the string library functions other than the ones you defined. The output should be the same as that of question 2 above.

The name of your C program file must be called lab9q4.c, files with any other name will not be marked.

Note and Ponder

  1. Assume that all file input is valid unless otherwise stated.

  2. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  3. There should be no blank lines at the start or end of each output run.

  4. You may want to check out the details of the implementation of other string library functions by invoking the following command in sunfire.

    $ man -s3c string
    

  5. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 41 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 30 different hosts have accessed this document in the last 441 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 10 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.