Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

HW 03 - Strings

The task is to write a simple program that reads a long multiword string with a predefined length. You should parse the string into a new array where the elements are the individual words. The space character is considered the delimiter. Then, use the qsort() function from stdlib.h library to lexicographically sort the words in ascending order. The program prints the sorted words, each on a separate line and terminates cleanly. Uppercase letters should be ordered before lowercase letters.

You will get the standard input of your program as follows

<number of characters>
<multiword string>

There will definitely be only a single space character between individual words.

There will be at least one word on the input.

Each word has at least one letter.

Your program is expected to allocate and deallocate used memory properly using malloc(), realloc(), free().

For sorting the array of words, use the function from stdlib.h
void qsort( void *ptr, size_t count, size_t size,
            int (*comp)(const void *, const void *) );
You can use string.h library for string operations.

You can find testing instances here. Files with the suffix .in contain your input with .out your desired output.

When you have a file pub00.in containing the program input, you can pass it to your program in like
$ cat pub00.in | ./main

Examples

Standard input Expected output
25
a bird came down the walk
a
bird
came
down
the
walk
44
there will be at least one word On the Input
Input
On
at
be
least
one
the
there
will
word

Evaluation

Upload your solution into BRUTE as a zip archive containing only your program's source files. This program will have to be simply compilable or provided with a Makefile or detailed description of the compilation process.

The maximum number of points is 10. No automatic evaluation, as you can test and verify your solution using the provided instances or generate your own.

  • 2 points - parsing of a string into an array of words
  • 3 points - words are sorted in ascending order
  • 2 points - correct memory (de)allocation
  • 3 points - coding style (structured, commented, separated into functions …)

Late submission

  • Submission is allowed one week after the deadline with the following penalties:
    • 50% point reduction
    • It is not possible to use string.h for string operations.
courses/be5b99cpl/hw/hw03.txt · Last modified: 2022/10/26 10:22 by ulricji1