Write a ‘C’ program to create a student structure having field stud name and address. Accept the details of ‘n’ students into the structure rearrange the data in alphabetical order of stud name and display the result.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char sname[20];
char add[20];
};
void main()
{
struct student s[10];
char ch[20];
int i,j,n;
clrscr();
printf("enter the student");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the sname&add");
for(i=0;i<n;i++)
{
scanf("%s%s",&s[i].sname,&s[i].add);
}
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(&s[i].sname,&s[j].sname)>0)
{
strcpy(ch,&s[i].sname);
strcpy(&s[i].sname,&s[i].sname);
strcpy(&s[j].sname,ch);
}
}
}
for(i=0;i<n;i++)
{
printf("%s",s[i].sname);
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char sname[20];
char add[20];
};
void main()
{
struct student s[10];
char ch[20];
int i,j,n;
clrscr();
printf("enter the student");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the sname&add");
for(i=0;i<n;i++)
{
scanf("%s%s",&s[i].sname,&s[i].add);
}
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(&s[i].sname,&s[j].sname)>0)
{
strcpy(ch,&s[i].sname);
strcpy(&s[i].sname,&s[i].sname);
strcpy(&s[j].sname,ch);
}
}
}
for(i=0;i<n;i++)
{
printf("%s",s[i].sname);
}
getch();
}