Sort the names and address into alphabetical order using structure

/*Sort the names into alphabetical order using structure*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char temp_name[50],temp_add[50];
int i,j,n;
struct people
{
char name[50];
char add[50];
}
p[200];
printf("Enter the number of people");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter name and address:");
scanf("%s%s",p[i].name,p[i].add);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(p[i].name,p[j].name)>0)
{
strcpy(temp_name,p[i].name);
strcpy(p[i].name,p[j].name);
strcpy(p[j].name,temp_name);
strcpy(temp_add,p[i].add);
strcpy(p[i].add,p[j].add);
strcpy(p[j].add,temp_add);
}
}
}
printf("\nName and Address in alphabetical order\n");
for(i=0;i<n;i++)
{
printf("\nName=%s\tAddress=%s",p[i].name,p[i].add);
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Enter Captcha Here : *

Reload Image