Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int char_comp(char c1, char c2) {
- if (c1 == c2)
- return 0;
- if (islower(c1) && isupper(c2))
- return -1;
- if (isupper(c1) && islower(c2))
- return 1;
- if (c1 < c2)
- return -1;
- return 1;
- }
- int comp(const void *s1, const void *s2) {
- int i = 0;
- while (1) {
- char c1 = *(*(char **)(s1) + i);
- char c2 = *(*(char **)(s2) + i);
- if (!c1 || !c2)
- break;
- int cmp = char_comp(c1, c2);
- if (cmp == 0) {
- ++i;
- continue;
- }
- return cmp;
- }
- char c1 = *(*(char **)(s1) + i);
- char c2 = *(*(char **)(s2) + i);
- if (!c1 && !c2)
- return 0;
- if (!c1)
- return -1;
- return 1;
- }
- int main(void) {
- int n;
- scanf("%d\n", &n);
- char **a = malloc(n * sizeof(char*));
- for (int i = 0; i < n; ++i) {
- int c;
- char *s = malloc(1);
- int sz = 0, mem_sz = 1;
- while (1) {
- c = getchar();
- if (c == '\n' || c == EOF)
- break;
- if (sz + 1 >= mem_sz) {
- mem_sz *= 2;
- char *s0 = realloc(s, mem_sz);
- s = s0;
- }
- s[sz] = c;
- ++sz;
- }
- if (sz + 1 >= mem_sz) {
- mem_sz += 1;
- char *s0 = realloc(s, mem_sz);
- s = s0;
- }
- s[sz] = '\0';
- a[i] = realloc(s, sz + 1);
- }
- qsort(a, n, sizeof(char*), comp);
- for (int i = 0; i < n; ++i) {
- printf("%s\n", a[i]);
- }
- free(a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement