C Program to Sort N Names in an Alphabetical Order

This is a C Program to sort the names in an alphabetical order.

Problem Description

The program will accept some names from the user as input & then sorts them in an alphabetical order using string operation.

Problem Solution

1. Create a 2D character array to store names of some fixed size.
2. Take names as input from users using for loop.
3. Now, sort this array of names using Selection sort.
4. Make a nested for loop, where the upper loop extracts each name and inner loop compares this name by the rest of the names below it.
5. After executing both the loops, rearranging all the names, finally an array of alphabetically sorted array will be obtained.

Program/Source Code

Here is source code of the C program to sort the names in an alphabetical order. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C program to read N names, store them in the form of an array
  3.      * and sort them in alphabetical order. Output the given names and
  4.      * the sorted names in two columns side by side.
  5.      */
  6.  
  7.     #include <stdio.h>
  8.     #include <string.h>
  9.     void main()
  10.     {
  11.  
  12.         char name[10][8], tname[10][8], temp[8];
  13.         int i, j, n;
  14.  
  15.         printf("Enter the value of n \n");
  16.         scanf("%d", &n);
  17.         printf("Enter %d names n \n", n);
  18.  
  19.         for (i = 0; i < n; i++) 
  20.         {
  21.             scanf("%s", name[i]);
  22.             strcpy(tname[i], name[i]);
  23.         }
  24.  
  25.         for (i = 0; i < n - 1 ; i++)
  26.         {
  27.             for (j = i + 1; j < n; j++)
  28.             {
  29.                 if (strcmp(name[i], name[j]) > 0) 
  30.                 {
  31.                     strcpy(temp, name[i]);
  32.                     strcpy(name[i], name[j]);
  33.                     strcpy(name[j], temp);
  34.                 }
  35.             }
  36.         }
  37.  
  38.         printf("\n----------------------------------------\n");
  39.         printf("Input NamestSorted names\n");
  40.         printf("------------------------------------------\n");
  41.  
  42.         for (i = 0; i < n; i++) 
  43.         {
  44.             printf("%s\t\t%s\n", tname[i], name[i]);
  45.         }
  46.  
  47.         printf("------------------------------------------\n");
  48.  
  49.     }
Program Explanation

1. Create two 2D character array of some fixed capacity.
2. Take the size of the array as input from the user, and fill the array with names taking them as input.
3. Now to sort this array, first make a nested for loop with i and j as iterators respectively.
4. The outer will will run from 0 to size – 1 , extracting each name of position i, one by one.
5. The inner loop will run from i+1 to size-1, comparing the name extracted by outer loop to all the names below it.
6. At each comparison, if the name above is alphabetically greater than the name below, then these two names are interchanged.
7. After executing the nested loop code section, we will obtain an array of names in proper alphabetical order.

advertisement
advertisement
Runtime Test Cases
Enter the value of n
7
Enter 7 names
heap
stack
queue
object
class
program
project
 
----------------------------------------
Input Names    Sorted names
------------------------------------------
heap           class
stack          heap
queue          object
object         program
class          project
program        queue
project        stack
------------------------------------------

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.