Computer programming - Lab 7

Strings

1. String to real Write a function similar to strtod which takes as parameter a string and returns the real number represented by that string. Accept as much of C syntax as possible: initial whitespace, optional sign, optional integer and fractional parts (not both missing), optional exponent and sign.

2. Ints in string Write a function that prints out all numbers (strings of digits with optional sign) from a string given as parameter. Consider only numbers that are not adjacent to any non-whitespace.

Input/Output

3. The Nth field Write a program that reads from input lines that contain numbers separated by commas (and possibly also whitespace), and prints the Nth number from each line. N is a constant defined in the program. Numbers may be missing between two commas, or there may be fewer than N numbers on a line. In either of these cases, print 0.

4. Searching for words Write a program that counts the number of nonoverlapping occurrences of a word in the input.
The word is a string constant in the program (or you may run the program with a command line argument, define main as int main(int argc, char *argv[]), check if argc == 2 and use argv[1]).
a) Consider only standalone words
b) Count also occurrences as substrings in other words
Handle the case where the words in the input may be arbitrarily long.

5. Words with line numbers Write a program that prints all lines from input that contain a given word. Only the part of the line starting with the word must be printed. Prefix it with the line number.
a) Consider only standalone words
b) Count also occurrences as substrings in other words

6. Running times Write a program that reads from input a list of finishing times in a race (given in increasing order), in the form h:mm:ss, one per line. Print the winning time on a line, and for each of the other finishing times, the difference with respect to the previous time, and with respect to the winning time. For example, for the input

3:51:38
4:05:47
4:11:28
the program should print
3:51:38
+0:14:09  0:14:09
+0:05:41  0:19:50

7. Structured text The .srt format for subtitles has the following structure. Each subtitle group is formed of four parts, on separate lines

  1. the group number (increasing)
  2. start and ending times for displaying subtitles, in the format hh:mm:ss,ttt --> hh:mm:ss,ttt (with thousandths of seconds).
  3. the actual subtitles, possibly on several lines
  4. a blank line as separator from the next group.
Write a program that reads input in .srt format and in the end prints the number of subtitle groups and the total time subtitles are displayed

8. Conjunctive normal form The DIMACS format for a logic formula in conjunctive normal form contains:

  1. any number (including 0) of comment lines starting with c
  2. a line defining the problem: p cnf followed by the number V of propositions (represented as numbers from 1 to V) and the number C of clauses
  3. the clauses, each given as a whitespace-separated list of numbers from -V to V (negated or positive variables) ending with a 0. A line should not simultaneously have opposite numbers (n and -n) and thus should have at most V nonzero numbers.
Read from input a formula in DIMACS CNF format, store it in an array of CxV numbers and then print each line (excluding zeroes).
Marius Minea
Last modified: Wed Nov 9 17:15:00 EET 2016