Computer programming - Lab 9

Strings and numbers

1. Write your own implementation of the strtod function: a) for floats in base 10; b) for hexadecimal floats.

2. Write a function that takes a string and prints out all numbers in the string. Prefix them with "int " or "float ", depending on their format (as in C). Print a) only numbers separated by whitespace from other text; b) all substrings interpretable as numbers.

String processing

3. Write a function that takes a string and prints all words in the string, each on one line. Words are formed of any characters except: whitespace, colon, comma, semicolon, period, question mark and exclamation mark. Use strspn / strcspn as needed.

4. Implement a function sscanprint with two parameters: the string to read from and the format string (like in sscanf), which simply prints the fields matched according to the format, one per line. Implement the format specifiers %d, %f and %MAXs, regular characters and whitespace. Return the same value as sscanf.

Strings and arrays of pointers

5. Options first Write a program that reorders the command-line argument array argv[], placing all strings that start with '-' first, followed by all other strings.

6. Split string Write a function that takes as parameter a string, an array of pointers and its length and fills the array with addresses to the beginning of each (whitespace-separated) word in the string (similar to the argv[] array of main). Terminate the array with a NULL pointer after the last valid address.

7. Concatenate strings Write a function that takes a NULL-terminated array of strings (string = char *), another string buf and a maximal length, and concatenates all strings from the array into buf, separated by one space, without exceeding the length limit. Avoid strcat and maintain a pointer to the end of the string being constructed.

8. Text lines Write a function that reads text from input into a character array (called buffer) and returns a NULL-terminated array of pointers to lines of the text read. The function has four parameters: a character array and an array of addresses, each with their respective size. The function should store the text read from input into the buffer, store the start of every line into the address array, and return when any of the arrays fills up (without reading more text than needed). Read the text directly at the right position into the buffer, to avoid useless copying.


Marius Minea
Last modified: Tue Nov 21 16:30:00 EET 2017