Search
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.
void qsort( void *ptr, size_t count, size_t size, int (*comp)(const void *, const void *) );
You can find testing instances here. Files with the suffix .in contain your input with .out your desired output.
.in
.out
pub00.in
$ cat pub00.in | ./main
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
-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.
<multiword string>
_bonus
main_bonus.c