Question: I need help coding in C. Maybe even older than grep, sort is another essential command-line utility of the UNIX ecosystem. This tool prints the

I need help coding in C.

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 example.

$ 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().

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!