/* Sort words lexicographically. */ #include #include #include #define MAXWORD 50 /* max word size */ #define N 1000 /* array size */ void sort_words(char *[], int); void swap(char **, char **); int main(void) { char *w[N]; /* an array of pointers */ char word[MAXWORD]; /* work space */ int n; /* number of words to be sorted */ int i; for (i = 0; scanf("%s", word) == 1; ++i) { if (i >= N) { printf("Sorry, at most %d words can be sorted.", N); exit(1); } w[i] = calloc(strlen(word) + 1, sizeof(char)); strcpy(w[i], word); } n = i; sort_words(w, n); for (i = 0; i < n; ++i) /* print the sorted words */ printf("%s\n", w[i]); return 0; }