Table of Contents

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 from the ranges: a-z, A-Z. Your program is expected to allocate and deallocate used memory properly using malloc(), realloc(), free(). You must not use preallocated fixed-size arrays or variable-length arrays.

For sorting the array of words, use the qsort() 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 with only your main.c file
  1. Evaluation draft: 26-27th October.
  2. Final evaluation: after the deadline.

The maximum number of points is 5. There is no automatic evaluation, as you can test and verify your solution using the provided instances or generate your own. Correct use of the memory is a must, and without it, you will lose points even if your program works flawlessly. Check your program with the Valgrind tool.

  • 2 point - parsing of a string into an array of words, words are sorted in ascending order and printed
  • 2 points - unique words are printed in ascending order with their occurrences
  • 1 point - coding style (structured, commented with doxygen style or similar, separated into functions …)
courses/be5b99cpl/hw/hw03.txt · Last modified: 2024/10/16 10:05 by janotjir