or specific tests (e.g. test #2) as follows:
- Write a program called match.c that
- reads a (sub)string on the command line and
- searches for that (sub)string in every 'word' on stdin.
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:
- you may use functions from the string library
- you may assume that stdin consists of a list of words: one word per line, and there are no blanks in words
- there may be empty lines, and there may be no words in the redirected file
- you may assume that every word is less than 200 characters long
-
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:
- -printable
- -upper
- -lower
- -digit
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:
- the character and its integer code are shown together (similar to the command ascii)
- notice that the format prints 8 codes per line (again, much like ascii)
- between the pairs of codes on a line is a tab character
- you may of course use the functions in the ctype.h library (as summarised on the C-ReferenceCard)
- be careful to avoid "magic numbers" in your code
-
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:
- unzip the package by entering the command:
unzip /home/cs1921/web/16s2/labs/lab04/lab.zip
|
On your home computer:
- download the file
lab.zip and
- unzip the package by double clicking on the file icon
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:
- Makefile: Used to make an executable called matrixTest, and to run the executable. Do not change.
- matrix.c: An incomplete library of functions that manipulate matrices
- matrix.h: A header file that lists the functions in the matrix implementation. Do not change.
- matrixTest.c: The 'tester'. It contains the main program that calls functions in the library. Do not change.
- matrix.data: A testcase. It contains 2 matrices. Do not change.
To start this exercise:
- Look at the Makefile to see what it does:
- You can enter the command make to build the executable matrixTest.
- This executable is a 'tester': it is intended to test the functions in the library (declared in matrix.h and defined in matrix.c).
- You can enter the command make test to run the tester with the testcase matrix.data.
- The tester will generate a read error because it is not able to read matrices as it stands.
- Look at the tester matrixTest.c and see what it does.
- It reads and prints 2 matrices, copies one of the matrices, and adds 2 matrices.
- The 2 matrices are chosen so that their sum results in a matrix consisting of a constant value.
- Look at the library matrix.c.
- Notice that only the functions printMatrix() and constMatrix() are complete.
- Incomplete are:
- a function to read a matrix from stdin and return success or failure
- a function to copy a matrix
- a function to add 2 matrices (it is assumed you know how to do this)
- a function to compare matrices and return whether they are equal or not.
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.