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. lab05 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 lab05 |
or specific tests (e.g. test #2) as follows:
prompt$ ~cs1921/bin/dryrun lab05 2 |
Exercises
- Write a program called mary.c that concatenates all its command-line arguments, including the name of the executable itself, into a single string on the heap. This string is then printed. Below are some testcases.
prompt$ ./mary 1 2 3 4 5 6 7 8 9 0 ./mary1234567890 |
Notice that there are no spaces. prompt$ ./mary had a little lamb, its fleece was white as snow ./maryhadalittlelamb,itsfleecewaswhiteassnow |
If there are no arguments: You may use the functions in the string library string.h. You may not use any fixed-length arrays. -
Write a program called heapsum.c that places all of its numerical command-line arguments on the heap, then sums them, and outputs the result. So for example, we have:
prompt$ ./heapsum 12 34 56 78 90270 |
If there are no arguments, the program does nothing. Your program should contain:
- a function to place each of the command-line arguments into the heap, resulting in a heap of numbers
- another function to sum all the numbers on the heap
The main() function should call both functions to compute the sum. Note further:
- you may assume that the command-line arguments are numerical
- you should store the numbers on the heap only
- you should not use any fixed-length arrays
-
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
fantasticdogxgooblygooblygooantxxantantanananananananananananan |
and we are searching for the (sub)string ant, then match outputs the following: ./match ant < match.datafantasticantxxantant |
These are the 4 words in the list that contain the (sub)string ant. Another testcase is: ./match x < match.dataxantxxant |
If there is no match, then the user is informed: ./match xyz < match.dataNo match found |
./match 123 < match.dataNo match found |
If the data file is empty, then there can also be no match: ./match anything < empty.dataNo match found |
If the usage is incorrect, this is also reported: ./match < match.dataUsage: ./match string |
./match bull ant < match.dataUsage: ./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. HawaiiHawaii'sHawaiianHawaiiansNaziismNaziismsPompeiiPompeii'salibiinggeniipiingradiiradii'ssafariingshanghaiingskiingskiing'staxiing |
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 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 -printable32 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 756 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ?64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w120 x 121 y 122 z 123 { 124 | 125 } 126 ~ |
With no option, it does printable: prompt$ ./asci32 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 756 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ?64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w120 x 121 y 122 z 123 { 124 | 125 } 126 ~ |
prompt$ ./asci -upper65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X89 Y 90 Z |
prompt$ ./asci -lower97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x121 y 122 z |
prompt$ ./asci -digit48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 756 8 57 9 |
If the option is spelt incorrectly: prompt$ ./asci -upUsage: ./asci [-printable,-upper,-lower,-digit] |
or if there are too many options: prompt$ ./asci -upper -lowerUsage: ./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
Submit your work (not before your scheduled lab please) using:
give cs1921 lab05 mary.c heapsum.c match.c asci.c