Friday, April 10, 2015

23. Write a program that reads different names and addresses into the computer and sorts the name into alphabetical order using structure variables.

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[30],address[50];
}s[50];

void main()
{
int i,j,n;
char temp_name[30],temp_add[50];
clrscr();
printf("\n How many students:-");
scanf("%d",&n);
printf("\n Input name and address of %d students",n);
for(i=0;i<n;i++)
{
fflush(stdin);
printf("\n Name [%d]:-",i+1);
gets(s[i].name);
printf("\n Address[%d]:-",i+1);
gets(s[i].address);
}

for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(s[i].name,s[j].name)>0)
{
strcpy(temp_name,s[i].name);

strcpy(s[i].name,s[j].name);

strcpy(s[j].name,temp_name);

strcpy(temp_add,s[j].address);

strcpy(s[i].address,s[j].address);

strcpy(s[j].address,temp_add);
  }
 }
}
printf("\n Information in Alphabetical order are: \n");
printf("\n Name \t Address");
for(i=0;i<n;i++)
printf("\n %s\t%s",s[i].name,s[i].address);
getch();
}

No comments:

Post a Comment