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
prompt$ ./mary had a little lamb, its fleece was white as snow ./maryhadalittlelamb,itsfleecewaswhiteassnow
prompt$ ./mary ./mary
-
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 1 2 36
prompt$ ./heapsum 12 34 56 78 90270
prompt$ ./heapsum 100100
prompt$ ./heapsumprompt$
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
./match ant < match.datafantasticantxxantant
./match x < match.dataxantxxant
./match xyz < match.dataNo match found
./match 123 < match.dataNo match found
./match anything < empty.dataNo match found
./match < match.dataUsage: ./match string
./match bull ant < match.dataUsage: ./match string
HawaiiHawaii'sHawaiianHawaiiansNaziismNaziismsPompeiiPompeii'salibiinggeniipiingradiiradii'ssafariingshanghaiingskiingskiing'staxiing
- 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
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 ~
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
prompt$ ./asci -upUsage: ./asci [-printable,-upper,-lower,-digit]
prompt$ ./asci -upper -lowerUsage: ./asci [-printable,-upper,-lower,-digit]
- 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