This program takes an integer value which then determines the amount of strings that can be inputted, once the user has entered the amount of strings that they specified they may input another integer and then input those amount of strings. Once done the program sorts the strings based on their lengths in descending order. However the qsort doesn't work, it ends up outputting how the order of when the strings were originally entered.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sort(const void * a, const void * b){
size_t fa = strlen((const char *)a);
size_t fb = strlen((const char *)b);
return (fa < fb) - (fa > fb);
}
int main(void){
char pointer[100];
int n;
scanf("%d", &n);
char** strings = malloc(n * sizeof(char*));
int i;
for (i = 0; i < n; i++){
scanf("%s", pointer);
strings[i] = malloc(sizeof(char) * (strlen(pointer) + 1));
strcpy(strings[i], pointer);
}
int m;
scanf("%d", &m);
strings = realloc(strings, (n + m) * sizeof(char*));
for (i = n; i < m + n; i++){
scanf("%s", pointer);
strings[i] = malloc(sizeof(char) * (strlen(pointer) + 1));
strcpy(strings[i], pointer);
}
int a;
int g;
int k = m + n;
qsort(strings , a, 100, sort);
for (g = 0; g < k; g++){
printf("%s", strings[g]);
printf("\n");
}
}