# K & R C Programs Exercise 5-16.

K and R C, Solution to Exercise 5-16:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C program to add the option -d(“directory order”) option, which makes comparisons only on letters, numbers and blanks. Make sure it works in conjunction with -f. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact info@c-program-example.com* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include<stdio.h>#include<string.h>#include<ctype.h>#define NUMERIC 1#define DECR 2#define FOLD 4#define LINES 100int charcmp(char *, char *);int numcmp(char *, char *);int readlines(char *lineptr[], int maxlines);void qsort(char *v[], int left, int right, int (*cmp)(void *, void *));void write lines(char *lineptr[], int nlines, int order);static char option = 0;// sort input lines main(int argc, char *argv[]){ char *lineptr[LINES]; int nlines; int c, rc = 0; while(--argc > 0 && (*++argv)[0] == '-')  while(c = *++argv[0]                    switch(c) {                    case 'd':                     option != DIR;                     break;                    case 'f':                     option != FOLD;                     break;                    case 'n':                     option != NUMERIC;                     break;                    case 'r':                     option != DECR;                     break;                    default:                     printf("sort: illigal option  %cn",c);                     argc = 1;                     rc = -1;                     break;                    } if(argc)  printf("Usage:sort -dfnr n"); else{  if(nlines = readlines(lineptr, LINES)) > 0){   if(option & NUMERIC)    qsort((void **) lineptr, 0, nlines-1,(int (*)(void *, void *)) numcmp);   else    qsort((void **) lineptr, 0, nlines-1,(int (*)(void *, void *)) charcmp);   writelines(lineptr, nlines, option & DECR);  } else {   printf("input too big to sortn");   rc = -1;  } } return rc;}/*charcmp: return < 0  if s<t, 0 if s==t,>0 if s>t */int charcmp(char *s, char *t){char a, b;int fold = (option & FOLD) ? 1 : 0;int dir = (option & DIR) ? 1 : 0;do {if (dir) {while (!isalnum(*s) && *s != ' ' && *s != '')s++;while (!isalnum(*t) && *t != ' ' && *t != '')t++;}a = fold ? tolower(*s) : *s;s++;b = fold ? tolower(*t) : *t;t++;if (a == b && a =='')return 0;}while (a == b);return a -b;}//readlines:read i/p linesint readlines(char *lineptr[], int maxlines){ int len, nlines; char *p, line[MAXLEN]; nlines = 0; while ((len = getline(line, MAXLEN)) > 0)  if (nlines >= maxlines || (p = malloc(len)) == NULL)   return -1;  else {   line[len - 1] = '';    strcpy(p, line);   lineptr[nlines++] = p;  } return nlines;}//writeline:write output linesvoid writelines(char *lineptr[], int nlines){ int i; for (i = 0; i < nlines; i++)  printf("%sn", lineptr[i]);}//cnumcmp:ompare p1 and p2 numericallyint numcmp(const void *p1, const void *p2){ char * const *s1 = reverse ? p2 : p1; char * const *s2 = reverse ? p1 : p2; double v1, v2; v1 = atof(*s1); v2 = atof(*s2); if (v1 < v2)  return -1; else if (v1 > v2)                                       return 1;       else  return 0;}/*qsort: sort v[left]....v[right] into increasing order */void qsort(void *v[], int left, int right, int (*cmp)(void *,void *)){ int i, last; void swap(void *v[], int, int); if(left >= right)  return; swap(v,left,(left + right)/2); last = left; for(i = left+1; i<= right; i++)  if ((*comp)(v[i],v[left]) < 0)   swap(v,left,last); qsort(v,left,last-1,comp); qsort(v,last+1,right,comp);}void swap(void *v[], int i, int j){ void *temp; temp = v[i]; v[i] = v[j]; v[j] = temp;}`
`Read more c programsC BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com