C Program To Sort Names in Descending Order
Learn How To Sort Names in Descending Order in C Programming Language. This C Program To Print Names in Reverse Order makes use of Dynamic Memory Allocation.
The strcmp() method is used to compare two different strings. It not only compares the initial characters but the whole string. The strcpy() method copies the string from one location to another.
Must Read: C Programs To Arrange Names in Alphabetical Order
C Program To Sort Names in Descending Order using Strings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char **name_list, array[100]; int m, n, limit; printf("\nEnter Total Names To Be Entered:\t"); scanf("%d", &limit); name_list = (char **)malloc(sizeof (char *) * limit); for(m = 0; m < limit; m++) { name_list[m] = (char *)malloc(sizeof (char) * 100); } printf("\nEnter The Names Sequentially\n"); getchar(); for(m = 0; m < limit; m++) { fgets(name_list[m], 100, stdin); } for(m = 0; m < limit - 1; m++) { for(n = m + 1; n < limit; n++) { if(strcmp(name_list[m], name_list[n]) < 0) { strcpy(array, name_list[m]); strcpy(name_list[m], name_list[n]); strcpy(name_list[n], array); } } } printf("\nDescending Order of Names:\n"); for(m = 0; m < limit; m++) { printf("%s", name_list[m]); } return 0; } |
Must Read: C Program To Print India’s Map (Obfucated Code)
Output

If you have any compilation error or doubts in this C Program To Sort Names in Descending Order, let us know about it in the Comment Section below.
Why can’t we use Array here to declare the string instead of using the malloc() function? It is quite difficult to implement in this Sorting String Names C Program.
You might be aware that Arrays are used for Static Allocation of Memory and malloc() function uses Dynamic Allocation of Memory. Yes, you can definitely used Arrays, no issues with that. I have used DMA to let the user fill his own memory size preference.