Warning
This page is located in archive.

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 also counts how many times each word appears in the text. The program prints the sorted words and their occurrence on a separate line and terminates cleanly. Uppercase letters should be ordered before lowercase letters.

Each line on the output should contain a number of occurrences and the appropriate word in the order separated by a single space.

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(). You should not use a preallocated stack memory.

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
1 a
1 bird
1 came
1 down
1 the
1 walk
56
word the be there will be at least one word On the Input
1 Input
1 On
1 at
2 be
1 least
1 one
2 the
1 there
1 will
2 word

Evaluation

Upload your solution into BRUTE as a zip archive containing only your program's source files and either a Makefile or readme explaining how to compile it
Your submission must compile without errors with the following compiler flags

-Wall -Werror -pedantic -std=c99 -O2

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 - only unique works are printed in ascending order with their occurrences
  • 2 points - correct memory (de)allocation
  • 1 point - coding style (structured, commented, separated into functions …)

Bonus

  • 3 points - modify your program so that it does not need to know the input size before reading the text input.
    • The input then should be expected to look like this

      <multiword string>
      

  • The submitted source file names should end with _bonus suffix e.g. main_bonus.c etc
courses/be5b99cpl/hw/hw03.txt · Last modified: 2023/10/19 18:08 by ulricji1