Thursday

Write a program that reads different names and addresses into the computer and rearrange them into alphabetical order using the structure variables.

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[30];
char add [30];
}std[100];
void main()
{
char tname[30],tadd[30];
int i,j,n;
printf("\n Enter how many students: ");
scanf("%d",&n);
printf("Enter names and addresses for %d srudents: ",n);
for(i=0;i<n;i++)
scanf("%s%s",std[i].name, std[i].add);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if (strcmp(std[i].name,std[j].name)>0)
{
strcpy(tname,std[i].name);
strcpy(std[i].name,std[j].name);
strcpy(std[j].name,tname);
strcpy(tadd,std[i].add);
strcpy(std[i].add,std[j].add);
strcpy(std[j].add,tadd);
}
}
}
printf("\n Sorted names in alphabetical order according to names are:\n");
for(i=0;i<n;i++)
printf("\n %s\t %s",std[i].name,std[i].add);
getch();
}

No comments:

Post a Comment

Rahul-Notes