C Program to Sort the N Names in an Alphabetical Order

Advertisements
This C Program sorts the names in an alphabetical order. The program accepts names & then sorts the names in an alphabetical order using string operation.

Here is source code of the C program to sort the names in an alphabetical order. The C program is successfully compiled and run on a Linux system. 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. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. void main()
  10. {
  11.     char name[10][8], tname[10][8], temp[8];
  12.     int i, j, n;
  13.  
  14.     printf("Enter the value of n \n");
  15.     scanf("%d", &n);
  16.     printf("Enter %d names n", \n);
  17.     for (i = 0; i < n; i++)
  18.     {
  19.         scanf("%s", name[i]);
  20.         strcpy(tname[i], name[i]);
  21.     }
  22.     for (i = 0; i < n - 1 ; i++)
  23.     {
  24.         for (j = i + 1; j < n; j++)
  25.         {
  26.             if (strcmp(name[i], name[j]) > 0)
  27.             {
  28.                 strcpy(temp, name[i]);
  29.                 strcpy(name[i], name[j]);
  30.                 strcpy(name[j], temp);
  31.             }
  32.         }
  33.     }
  34.     printf("\n----------------------------------------\n");
  35.     printf("Input NamestSorted names\n");
  36.     printf("------------------------------------------\n");
  37.     for (i = 0; i < n; i++)
  38.     {
  39.         printf("%s\t\t%s\n", tname[i], name[i]);
  40.     }
  41.     printf("------------------------------------------\n");
  42. }


$ cc pgm32.c
$ a.out
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 Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.
Advertisements
Subscribe Newsletter & Posts

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

Practice Interview Questions & Answers, Quizzes, Objective, Multiple Choice Aptitude Tests for Freshers and Experienced People in these topics - Learn C, C++, Java and C# Programming with coding example on Simple problems as well as tough Algorithms and Data-structures along with runtime output: - C Programs, Data-Structures and Algorithms, C++ Algorithms, Java Algorithms, C# Programs, Android Programs in Java

C Tutorials and Linux Commands Tutorial - C Tutorials, Linux Commands

Career Mentoring with our Founder / CTO - Read More
Advertisements

Follow Manish & Sanfoundry