Code In Tech - The Ultimate World of Computer Technology

Saturday 24 March 2012

C Program to Arrange the given Names in Alphabetical order using Array

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char name[40][100],temp[20];
 int i,j,n;
 clrscr();
 printf("Enter the total number of names\n");
 scanf("%d",&n);

 printf("\nEnter the names one by one.....");
 for(i=0;i<n;i++)
  {
   scanf("%s",name[i]);
  }
 printf("\nNames in alphebetical order\n\n");
  for(i=1;i<n;i++)
  {
   for(j=1;j<n;j++)
     {
      if((strcmp(name[j-1],name[j]))>0)
       {
    strcpy(temp,name[j-1]);
    strcpy(name[j-1],name[j]);
    strcpy(name[j],temp);
       }
     }
  }
 for(i=0;i<n;i++)
    printf("\t%d)%s\n",i+1,name[i]);
getch();
}

No comments :

Post a Comment