C Programming
Computer Programming

C program to create a student structure and rearrange the data in alphabetical order



Write a C program to create a student structure having fields stud_name and address. Accept the details of 'n' students, rearrange the data in alphabetical order of student name and display it

\* C program to create a student structure and rearrange the data in alphabetical order *\

# include < stdio.h >
# include < string.h >
struct   stud
{

char name[50] ;
char add[50] ;

} s[100] ;

int   main( )
{

struct stud t ;
int i = 0, j = 0, n ;
printf("\n /*How many student records you want to enter?*/") ;
printf("\n\n Enter Limit : ") ;
scanf("%d", &n) ;
for( i = 0 ; i < n ; i++ )
{
      printf("\n Enter Student-%d Details", i+1 ) ;
      printf("\n --------------------------\n" ) ;
      printf(" Enter Name : " ) ;
      scanf("%s", s[i].name ) ;
      printf(" Address : " ) ;
      scanf("%s", s[i].add ) ;
}

printf("\n\t Data before rearrangement" ) ;
printf("\n -----------------------------------\n" ) ;
printf(" Student Name\tAddress\n" ) ;
printf("--------------------------------------\n" ) ;
for( i = 0 ; i < n ; i++ )
{
      printf("\n%-10s\t%3s\n", s[i].name, s[i].add ) ;
}

for( i = 0 ; i < n ; i++ )
{
      for( j = i + 1 ; j < n ; j++ )
      {
            if(strcmp( s[i].name, s[j].name ) > 0 )
            {
                  t = s[i] ;
                  s[i] = s[j] ;
                  s[j] = t ;
            }
      }
}
printf("\n\t Data after rearrangement" ) ;
printf("\n -----------------------------------\n" ) ;
printf(" Student Name\tAddress\n" ) ;
printf("--------------------------------------\n" ) ;
for( i = 0 ; i < n ; i++ )
{
      printf("\n%-10s\t%3s\n", s[i].name, s[i].add ) ;
}
return 0 ;

}

Output of Program :

Output of C program to create a student structure and rearrange the data in alphabetical order