Computer programming - Lab 3

Processing numbers

1. Write a function that prints a number, digit by digit, in base 16.

2. Write a function that decomposes a number into prime factors, and prints out the result, in the form: 18=2*3^2
Warning, ^ does *not* denote exponentiation in C.

Character processing

3. Write a program that counts the words in current input line. A word is a sequence of characters which are not whitespace (isspace()).

4. Write the wordcount program that counts the number of characters, words, and lines in the input.

5. Write a program that prints its input with C-style comments filtered out. (both multiline and single-line comment syntax).

6. In LaTeX, a command consists of a backslash, an identifier (letters only), and an optional sequence of arguments placed within braces {}, e.g. \setcounter{page}{3} . Write a program that filters out any LaTeX commands in standard input, printing only the rest of the text.

7. An SVG path consists of the following commands: L or l (draws a line from the current point to the indicated coordinates), M or m (moves the current point), each followed by two real numbers, representing x and y coordinates (absolute for uppercase commands, relative for lowercase). Commands are separated by whitespace. Write a program that reads a sequence of commands and computes the total length of the path drawn. Initial coordinates are assumed (0, 0). You may use the following function to read a number:

double readf(void)
{
  double x = 0;
  scanf("%lf", &x);
  return x;
}

8. An XML file contains tags delimited by < and > . A start tag starts with < . An end tag starts with </ . Both end with > . An empty-element tag starts with < and ends with /> . Inside these delimiters, a tag may contain first a name, and then a whitespace-separated list of attributes, which are pairs of the form name=value, for example <img src="file.jpg" alt='some text'/>.
a) Write a program that reads an XML-structured input and prints out all tag names and attribute names, one line per tag (example: img src alt).
b) (optional). Check that start tags and end tags are correctly nested (with matching names). To avoid working with strings, you can hash strings to integers; use for example a function that updates hash = 33*hash + c for each character in the string.

Marius Minea
Last modified: Fri Oct 11 13:40:00 EEST 2013