Week 4 Laboratory — Chars, Strings and Other Arrays

We have created some scripts that can automatically run your program against some tests. To run these tests you can execute the dry run program with an argument that corresponds to the lab and week, i.e. lab04 for this week. It expects to find all the programs to be submitted as part of this lab in the current directory. You can use dry run as follows:

prompt$ ~cs1921/bin/dryrun lab04

or specific tests (e.g. test #2) as follows:

prompt$ ~cs1921/bin/dryrun lab04 2

Exercises

  1. Write a program called match.c that

    Every word that contains this (sub)string is printed. If no match can be found, an appropriate message is output. Also, if there is less or more than one command-line argument, a usage-message is output.

    For example, if the file match.data contains the words

    fantastic
    dog
    x
    gooblygooblygoo
    antx
    xant
    ant
    anananananananananananan
    

    and, redirecting stdin to this file, we are searching for the (sub)string ant, then match outputs the following:

    ./match ant < match.data
    fantastic
    antx
    xant
    ant
    

    These are the 4 words in the list that contain the (sub)string ant. Another testcase is:

    ./match x < match.data
    x
    antx
    xant
    

    If there is no match, then the user is informed:

    ./match xyz < match.data
    No match found
    

    ./match 123 < match.data
    No match found
    

    If the data file is empty, then there can also be no match:

    ./match anything < empty.data
    No match found
    

    If the usage is incorrect, this is also reported:

    ./match < match.data
    Usage: ./match string
    

    ./match bull ant < match.data
    Usage: ./match string
    

    The final testcase is the list of dictionary words in the file /usr/share/dict/words: The output is below; you may work out for yourself what the command is.

    Hawaii
    Hawaii's
    Hawaiian
    Hawaiians
    Naziism
    Naziisms
    Pompeii
    Pompeii's
    alibiing
    genii
    piing
    radii
    radii's
    safariing
    shanghaiing
    skiing
    skiing's
    taxiing
    

    You should note:
  2. Write a program called asci.c that prints the ASCII codes of printable characters, uppercase characters, lowercase characters, and of digits, depending on a command-line option. The options are:

    If no option is set, then -printable is the default. It the option is incorrect, or there are too many arguments on the command line, then a usage message is output. The possible behaviours are:

    prompt$ ./asci -printable
    32  	33 !	34 "	35 #	36 $	37 %	38 &	39 '
    40 (	41 )	42 *	43 +	44 ,	45 -	46 .	47 /
    48 0	49 1	50 2	51 3	52 4	53 5	54 6	55 7
    56 8	57 9	58 :	59 ;	60 <	61 =	62 >	63 ?
    64 @	65 A	66 B	67 C	68 D	69 E	70 F	71 G
    72 H	73 I	74 J	75 K	76 L	77 M	78 N	79 O
    80 P	81 Q	82 R	83 S	84 T	85 U	86 V	87 W
    88 X	89 Y	90 Z	91 [	92 \	93 ]	94 ^	95 _
    96 `	97 a	98 b	99 c	100 d	101 e	102 f	103 g
    104 h	105 i	106 j	107 k	108 l	109 m	110 n	111 o
    112 p	113 q	114 r	115 s	116 t	117 u	118 v	119 w
    120 x	121 y	122 z	123 {	124 |	125 }	126 ~
    

    With no option, it does printable:

    prompt$ ./asci
    32  	33 !	34 "	35 #	36 $	37 %	38 &	39 '
    40 (	41 )	42 *	43 +	44 ,	45 -	46 .	47 /
    48 0	49 1	50 2	51 3	52 4	53 5	54 6	55 7
    56 8	57 9	58 :	59 ;	60 <	61 =	62 >	63 ?
    64 @	65 A	66 B	67 C	68 D	69 E	70 F	71 G
    72 H	73 I	74 J	75 K	76 L	77 M	78 N	79 O
    80 P	81 Q	82 R	83 S	84 T	85 U	86 V	87 W
    88 X	89 Y	90 Z	91 [	92 \	93 ]	94 ^	95 _
    96 `	97 a	98 b	99 c	100 d	101 e	102 f	103 g
    104 h	105 i	106 j	107 k	108 l	109 m	110 n	111 o
    112 p	113 q	114 r	115 s	116 t	117 u	118 v	119 w
    120 x	121 y	122 z	123 {	124 |	125 }	126 ~
    

    prompt$ ./asci -upper
    65 A	66 B	67 C	68 D	69 E	70 F	71 G	72 H
    73 I	74 J	75 K	76 L	77 M	78 N	79 O	80 P
    81 Q	82 R	83 S	84 T	85 U	86 V	87 W	88 X
    89 Y	90 Z
    

    prompt$ ./asci -lower
    97 a	98 b	99 c	100 d	101 e	102 f	103 g	104 h
    105 i	106 j	107 k	108 l	109 m	110 n	111 o	112 p
    113 q	114 r	115 s	116 t	117 u	118 v	119 w	120 x
    121 y	122 z
    

    prompt$ ./asci -digit
    48 0	49 1	50 2	51 3	52 4	53 5	54 6	55 7
    56 8	57 9
    

    If the option is spelt incorrectly:

    prompt$ ./asci -up
    Usage: ./asci [-printable,-upper,-lower,-digit]
    

    or if there are too many options:

    prompt$ ./asci -upper -lower
    Usage: ./asci [-printable,-upper,-lower,-digit]
    

    Some comments:
  3. A zip file is a way of compressing and packaging files. In this exercise you need to add code to a program that has been packaged in a zip file called lab.zip. To access the program you need to,

    From CSE computer:

    On your home computer:

    The above command will unpack and uncompress the files into your current directory, and indicate as follows:

    inflating: Makefile
    inflating: matrix.c
    inflating: matrix.data
    inflating: matrix.h
    inflating: matrixTest.c
    

    Here is a brief explanation of each of the files:

    To start this exercise:

    Your exercise is to write each of the above functions. The only file that you can submit in this exercise is matrix.c, and this is the only file that you should modify.

On conclusion of all the exercises submit your work using:


  give  cs1921  lab04  match.c  asci.c  matrix.c