1 Changelog
2 General information
3 Introduction
4 Programming exercises
1 Changelog
v1: First publication
2 General information
Due before 11:59 PM, Thursday May 31th, 2018
You are to work alone for this
The reference work environment is the CSIF
3 Introduction
3.1 Reference executables
Working executables for all the following problems are available on the CSIF class account, in directory /home/cs30/public/hw6.
4 Programming exercises
4.1 Simple grep
First released 43 years ago (!), grep is one of the oldest command-line utilities in the UNIX ecosystem.
grep is used to print the lines that it receives if they match a certain pattern. This tool makes it possible to detect if a file contains a certain word for ple, and if so, optionally at which line(s).
Write program simple_grep.c that reads entire lines from the user, and only prints back those which match any of the patterns received as program arguments, as illustrated in the following ple.
$ cat udhr_art26.txt (1) Everyone has the right to education. Education shall be free, at least in the elementary and fundamental stages. Elementary education shall be compulsory. Technical and professional education shall be made generally available and higher education shall be equally accessible to all on the basis of merit. (2) Education shall be directed to the full development of the human personality and to the strengthening of respect for human rights and fundamental freedoms. It shall promote understanding, tolerance and friendship among all nations, racial or religious groups, and shall further the activities of the United Nations for the maintenance of peace. (3) Parents have a prior right to choose the kind of education that shall be given to their children. $ ./simple_grep right FREEDOM "their children" < udhr_art26.txt (1) Everyone has the right to education. Education shall be for human rights and fundamental freedoms. It shall promote (3) Parents have a prior right to choose the kind of education that shall be given to their children. $ ./simple_grep -n right FREEDOM "their children" < udhr_art26.txt 1:(1) Everyone has the right to education. Education shall be 9:for human rights and fundamental freedoms. It shall promote 13:(3) Parents have a prior right to choose the kind of 14:education that shall be given to their children. $ ./simple_grep Usage: grep [-n] word … $ echo $? 1 $
Here are a list of requirements, assumptions and hints:
This program shall contain no global variables.
We assume that the maximum number of characters a line can contain is 80.
You will probably need to split the problem into a hierarchy of functions. A possible set of functions could include:
A function that returns whether a line matches a certain pattern.
A function that returns whether a line matches any of pattern in a collectin of pattens (such a function would call the first one).
The main function should be the brain, reading the line from the user and calling the matching function before displaying the line if it is a match.
If there is no given argument on the command, the program should display the command’s usage and return 1.
If the first command-line argument is -n, then the line number should be displayed before the matching lines.
In order to match strings while ignoring their case, you can use strncasecmp() function provided by the libc.
List of some important libc functions that are used in the reference program: strlen(), strncasecmp(), strcmp(), fgets().
4.2 French verb conjugator
A group of students at UC Davis is about to start a Study Abroad program in France, over the summer. They need to quickly learn a bit a French so that they can interact with people.
It would therefore be very helpful if they had access to a program that shows some of the verb conjugations, first starting with the easiest group of verbs: the verbs ending in -er.
For donner, which means to give in its infinitive form, is conjugated as follows:
Je donne (I give)
Tu donnes (You give)
Elle/Il donne (She/He gives)
Nous donnons (We give)
Vous donnez (You give –formal form)
Elles/Ils donnent (They give)
As you can observe, the suffix -er is first removed from the infinitive form and then a specific ending is added to the verb’s root according to the given pronoun.
Write program conjugueur.c that receives an -er French verb from the user and conjugates it accordingly, as illustrated in the following ple.
$ ./conjugueur Enter a French verb (-er): donner je donne tu donnes elle/il donne nous donnons vous donnez elles/ils donnent Conjugate another verb? [Yy] y Enter a French verb (-er): parler je parle tu parles elle/il parle nous parlons vous parlez elles/ils parlent Conjugate another verb? [Yy] n $ ./conjugueur Enter a French verb (-er): savoir Not a correct -er verb Conjugate another verb? [Yy] n $
Here are a list of requirements, assumptions and hints:
This program shall contain no global variables.
We assume that the maximum number of characters a verb (in its infinitive form or conjugated form) can contain is 30.
You will probably need to split the problem into a hierarchy of functions. A possible set of functions could include:
For this function, it might be a good idea to have the pronouns and endings in arrays of strings.
A function that conjugates a verb.
A function that returns whether a verb ends in -er or not.
The main function should be the brain, reading the verb from the user and calling the conjugation function if the verb is valid, before asking if another verb should be inputted.
Since the user is only requested to enter a verb, you can safely use scanf() to read the verb (instead of using fgets() which would be more difficult to handle as it keeps the newline character as part of the input buffer).
List of some important libc functions that are used in the reference program: strlen(), strncpy(), strcat(), scanf().
4.3 Simple sort
Maybe even older than grep, sort is another essential command-line utility of the UNIX ecosystem.
This tool prints the lines it received on its input in sorted order.
Write program simple_sort.c that first gets the number of lines it should read, and then prints the collection of lines it read in sorted order, as illustrated in the following ple.
$ cat udhr_art23.txt 9 (1) Everyone has the right to work, to free choice of employment, to just and favourable conditions of work and to protection against unemployment. (2) Everyone, without any discrimination, has the right to equal pay for equal work. (3) Everyone who works has the right to just and favourable remuneration ensuring for himself and his family an existence worthy of human dignity, and supplemented, if necessary, by other means of social protection. (4) Everyone has the right to form and to join trade unions for the protection of his interests. $ ./simple_sort < udhr_art23.txt (1) Everyone has the right to work, to free choice of employment, to just and (2) Everyone, without any discrimination, has the right to equal pay for equal (3) Everyone who works has the right to just and favourable remuneration (4) Everyone has the right to form and to join trade unions for the protection ensuring for himself and his family an existence worthy of human dignity, and favourable conditions of work and to protection against unemployment. of his interests. supplemented, if necessary, by other means of social protection. work. $ ./simple_sort 0 $ ./simple_sort 2000000000 malloc failed $ cat wrong_file 2 toto $ ./simple_sort < wrong_file Input error $ ./simple_sort a Invalid input $ ./simple_sort -1 Invalid input $ echo $? 1 $
Here are a list of requirements, assumptions and hints:
This program shall contain no global variables.
We assume that the maximum number of characters a line can contain is 80.
You must implement the following functions:
int get_nlines(void)
This function should return the number of lines to be read subsequently. The number must be a positive number (or 0), otherwise an error message should be displayed and the program should exit with error code 1.
char **get_lines(int nlines)
This function must dynamically allocate an array of strings of size nlines, and read the lines from the user in each array element.
Refer to the lecture slides to see how to dynamically allocate arrays of strings.
Any memory allocation or reading errors (e.g. less lines than announced) should be properly caught and cause the program to display the appropriate error message and exit with error code 1.
void sort_lines(char **lines, int nlines)
This function must sort the array of strings.
The strings themselves should not be modified, they should only be moved in the array.
void print_lines(char **lines, int nlines)
This function should print the entire array of strings.
void free_lines(char **lines, int nlines)
This function should free the entire array of strings.
The main function should be the brain, simply calling all the functions defined above in the correct order.
List of some important libc functions that are used in the reference program: fgets(), sscanf(), exit(), malloc(), free(), strcasecmp().
image.png