Write a C program that sorts a sequence of strings, in ASCII order (as you'd get using the "strcmp" library function).

Details

The number of strings to sort will be provided as the first argument to your program.

The strings will be provided one per line on standard input. (i.e. read with fgets(..., ..., stdin))

The sorted strings should be output one per line on standard output.

You are free to use any standard C library functions to write your program - including qsort, which will sort for you.

Do not use insertion sort.

See hints.txt for some extra ideas.

The clang-check tool should show no warnings / errors on your source.

Running your program under valgrind should show no memory leaks.

== HW04 Hints ==

  • You can read a line from standard input with "fgets(buf, max, stdin)" see: man 3 fgets
  • You can allocate your input buffer as an array on the stack. No test string will be longer than 80 characters.
  • You can sort with the "qsort" library function. see: man 3 qsort
  • You can compare two strings with "strcmp" see: man 3 strcmp
  • You'll need to write a wrapper function around strcmp to make the types match, since strcmp works with strings and qsort expects void*'s.

== HW04x Hints ==

  • I recommend building a dynamic array data structure (struct, functions) to store strings. https://en.wikipedia.org/wiki/Dynamic_array
  • End of file can be detected with feof(stdin) man feof
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.