/* mp5.c -- A solution written in C, using an array */ #include #include #include /* some test data */ const char const * mp5data = "fortnight.\n\0came \0eve" "ry \0down \0bug \0A \0"; /* a useful type to mae the code a bit more readable */ typedef const char * stringptr; /* in-place sort an array of stringptrs */ void sort( stringptr * array, int elements ) { /* the simplest known sort, see https://arxiv.org/pdf/2110.01111.pdf */ int i, j; for (i = 0; i < elements; i++) { for (j = 0; j < elements; j++) { if (strcmp( array[i], array[j] ) < 0) { stringptr t = array[i]; array[i] = array[j]; array[j] = t; } } } } /* main program to read and print mp5data */ int main() { stringptr p = mp5data; stringptr * a; /* pointer to an array of string pointers */ int size = 0; /* size of the array *a */ int i; /* index into *a */ while (*p != '\0') { /* how many "words" are there? */ size = size + 1; p = p + strlen( p ) + 1; } a = malloc( size * sizeof( stringptr ) ); p = mp5data; for (i = 0; i < size; i++) { /* get the "words" */ a[i] = p; p = p + strlen( p ) + 1; } sort( a, size ); for (i = 0; i < size; i++) { /* print the "words" */ printf( "%s", a[i] ); } }