This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
sys_prog/sortstrings/sortstrings_lib.c
2019-10-11 09:29:40 +02:00

30 lines
729 B
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_N 1000
int compar(const void* string1, const void* string2) {
const char** s1 = (const char**) string1;
const char** s2 = (const char**) string2;
return strcmp(*s1, *s2);
}
void sort_strings(const char* input, const char* output, unsigned int n) {
const char* strings[MAX_N];
size_t i = 0;
size_t offset = 0;
for (i = 0; i < n; i++) {
strings[i] = input + offset;
offset += strlen(input + offset) + 1; // + 1 for NUL
}
qsort(strings, n, sizeof(const char*), &compar);
offset = 0;
for (i = 0; i < n; i++) {
const size_t len = strlen(strings[i]);
strncpy((char*) output + offset, (char*) strings[i], len + 1);
offset += len + 1;
}
}