C Program To Sort Names in Alphabetical Order
Learn How To Sort Names in Alphabetical Order in C Programming Language. This C Program To Arrange Names in Ascending Order makes use of Dynamic Memory Allocation.
This program takes in an input of Array of Characters, also known as Strings.The strcmp() method is used to compare two different strings. It not only compares the first or initial characters but the whole string. The strcpy() method copies the string from one location to another. The malloc() method used here allocates the requested memory by the User and returns a pointer to it.
Must Read: Sort Names in Descending Order in C Programming
C Program To Sort Names in Alphabetical Order
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("\nAscending Order of Names:\n"); for(m = 0; m < limit; m++) { printf("%s", name_list[m]); } return 0; } |
Must Read: C Program To Display Live Digital Clock
Output

If you have any compilation error or doubts in this C Program To Sort Names in Alphabetical Order, let us know about it in the Comment Section below.
This program works quite good and is very easy to understand. Can you tell me if there is library function to arrange the elements in alphabetical order automatically?
This is really an easy C program to print strings alphabetically. Thanks for this one.
without string function ,will you please explain ? using while loop only?