Monday, May 2, 2016

Sort Names In Alphabetical Order

Write a C program to read N names, store them in an array and sort them in alphabetical order. 

#include <stdio.h>
#include <conio.h>
#include <string.h>

int main()
{
   char name[10][8], old_name[10][8], swap[8];
   int i, j, n;

   printf("Enter the value of N :\n");
   scanf("%d", &n);

   for(i=0; i< n ; i++)
   {
    printf("Enter name number %d :\n",i+1);
 scanf("%s",name[i]);
 strcpy (old_name[i], name[i]);
   }

   for(i=0; i < n-1 ; i++)
   {
 for(j=i+1; j< n ; j++)
 {
    if(strcmpi(name[i],name[j]) > 0)
    {
        strcpy(swap,name[i]);
        strcpy(name[i],name[j]);
        strcpy(name[j],swap);
    }
 }
   }
    printf("\n\nNames Before Sorting :\n");
    for(i=0; i< n ; i++)
   {
 printf("%s\n",old_name[i]);
   }
    printf("\n\nNames After Sorting :\n");
   for(i=0; i< n ; i++)
   {
 printf("%s\n",name[i]);
   }

    return 0 ;
}


Share this

0 Comment to "Sort Names In Alphabetical Order"

Post a Comment