// sort string
// test_sort_str.c
// victor00000 (c) 2018.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// ~$ gcc test_sort_str.c -o test_sort_str
// ~$ ./test_sort_str /* c a b d
// ~$ ./test_sort_str -r /* c a b d
int main (int ia, char *ca[]){
if ( ia == 1 ) return 0;
char **c = malloc (sizeof (char*));
c[0] = 0;
int i, s = -1, l = 0;
for (i=1;i<ia;i++){
if (strcmp ("-r",ca[i]) == 0){
s = 1;
continue;
}
c[l] = strdup (ca[i]);
l++;
c = realloc (c,(l+1)*sizeof (char*));
c[l] = 0;
}
if (c[0] == 0) {free(c);return 0;}
i=0;
char *tmp;
while (c[i]!=0){
l=0;
while (c[l]!=0){
if (strcmp (c[i],c[l]) == s){
tmp = c[i];
c[i] = c[l];
c[l] = tmp;
}
l++;
}
i++;
}
i=0;
while (c[i]!=0){
printf ("%s\n",c[i]);
free (c[i]);
i++;
}
free (c);
return 0;
}