#include #include #define TRUE 1 #define FALSE 0 /* compare two strings */ /* return 1 if first string is greater, -1 if second one is greater and 0 if they are equal */ int compare (const void *sv1, const void *sv2) { char *s1 = *((char **) sv1); /* void * is actually a char ** */ char *s2 = *((char **) sv2); while (TRUE) { if (*s1 == '\0') { if (*s2 == '\0') { return 0; } return -1; } if (*s2 == '\0') { return 1; } if (*s1 < *s2) { return -1; } if (*s1 > *s2) { return 1; } s1++; s2++; } } int main (int argc, char *argv[]) { int i; for (i = 0; i < (argc - 1); i++) { printf ("The next argument is %s\n", argv[i+1]); } /* sort the arguments by using a library function */ qsort (&(argv[1]), argc - 1, sizeof (char *), compare); for (i = 0; i < (argc - 1); i++) { printf ("The next sorted argument is %s\n", argv[i+1]); } exit (0); }