Computer programming - Lab 9

Strings and arrays of pointers

See also problems 6 and 7 from Lab 8

1. Text lines Write a function that reads text from input into a buffer and returns a NULL-terminated array of pointers to lines of the text read. The function has four parameters: a text buffer and an array of addresses, each with their respective size. The function should store the text read from input into the buffer, storing the start of every line into the address array, and returning when any of the arrays fills up. Read the text directly at the right position into the buffer, avoiding useless copying.

Type casts and void *

See also problem 8 from Lab 8

2. Print from address array Write a function int myprintf(const char *fmt, void *adrtab[]) that works like printf, but receives a NULL-terminated array of addresses (void *) of objects to be printed.
Implement the format specifiers %d, %f and %s, as well as printing regular characters and %. An error encountered in the format should terminate printing.

3. Read to address array Write a function int myscanf(const char *fmt, void *adrtab[]) that works like scanf, but receives a NULL-terminated array of addresses (void *) of objects to be read.
Implement the format specifiers %d, %lf and %MAXs (where MAX is an unsigned constant) as well as matching regular characters and whitespace. An error encountered in the format should terminate reading.

Function parameters

4. Countif Write a function that counts how many elements in an array satisfy a given condition.
The function takes as parameters an array, its element count, element size, and a pointer to a function. That function takes as parameter a pointer to an element (as void *) and returns nonzero (meaning the element satisfies the desired condition) or zero (it does not). Test your function in a program.

5. Map Write a function that changes all elements of an array by calling a function.
The function takes as parameters an array, its element count, element size, and a pointer to a function. That function takes as parameter a pointer to an element (as void *) and changes the value of the element. Test your function in a program.

6. Partition Write a function that rearranges the elements of an array: first all that satisfy a given property, and then all that don't.
The function takes as parameters an array, its element count, element size, and a pointer to a function. That function takes as parameter a pointer to an element (as void *) and returns nonzero (meaning the element satisfies the desired condition) or zero (it does not). Test your function in a program. For example, you could group elements of an array that are less than / not less than a given value (this partitioning step is used in quicksort).

Recursive string processing

7. Integer expression Write a function that returns the value of an integer expression with arithmetic operators and parantheses, read from a string. The address of the string address (char **) is passed as parameter; on return, that address should contain the address of any remainder of the string. You may adapt the similar program that reads the expression from input.

8. Nested tags Write a function that takes as argument a string with XML text and checks that tags are properly nested. An opening tag is <tagname> and a closing tag is </tagname> . There can also be arbitrary text without < and > . Return 1 or 0 and print the error location.
Hint: On each recursion level keep a pointer to the opening tag (and perhaps its length) and check the same name appears in the closing tag.


Marius Minea
Last modified: Wed Nov 23 16:10:00 EET 2016