Friday, April 10, 2015

5. Write a program that reads names and address into the computer and re arrange the name into alphabetical order using structure variables.


#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[50];
char address[50];
};
struct student s[100];
void main()
{
int i,j,n;
char temp[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;i++)
{
for(j=i+1;j<n;j++){
if(strcmp(s[i].name,s[j]).name)>0
{
strcpy(temp,s[i].name);
strcpy(s[i].name,s[j].name);
strcpy(s[j].name,temp);
strcpy(temp,s[i].address);
strcpy(s[i].address,s[j].address);
strcpy(s[j].address,temp);
}
}
}
printf("\n Sorted list:-\n");
printf("\n Name\t address\n");
for(i=0;i<n;i++)
 printf("\n%s \t %s ",s[i].name,s[i].address);
getch();
}

3 comments:

  1. not working any more
    try to rewrite the corrected code

    ReplyDelete
  2. this program does not show output

    ReplyDelete