Question Name:Sorted Student List

#include <stdio.h>
#include<string.h>
struct aviraj 
{ 
  int a;
  char c[50];
} s1[20];
int main()
{
int l,i,j,tep;
  char temp [50];
  scanf("%d",&l);
  for(i=0;i<l;i++) { 
    scanf("%s %d",s1[i].c,&s1[i].a); 
  }
  for (i = 0; i < l - 1 ; i++)
{
for (j = i + 1; j < l; j++)
{
if (strcmp(s1[i].c, s1[j].c) > 0)
{
strcpy(temp, s1[i].c); tep=s1[i].a;

strcpy(s1[i].c, s1[j].c); s1[i].a=s1[j].a;
strcpy(s1[j].c, temp); s1[j].a=tep;
}
}
  }
  printf("After sorting\nName ID\n");
   for(i=0;i<l;i++) { 
   printf("%s %d\n",s1[i].c,s1[i].a); 
   }
	return 0;
}

Problem Description

In CSE Department, there are 10 students, where students list was in the enrollment order. You have to prepare list students in the alphabetical order.

  • Test Case 1

    Input (stdin)

    10
    Anu 87
    Rahul 95
    Atul 21
    Ram 101
    Rithwik 45
    saleem 55
    Amit 88
    Jancy 66
    Jibin 65
    Ashwin 75
    

    Expected Output

    After sorting
    Name ID
    Amit 88
    Anu 87
    Ashwin 75
    Atul 21
    Jancy 66
    Jibin 65
    Rahul 95
    Ram 101
    Rithwik 45
    saleem 55
  • Test Case 2

    Input (stdin)

    8
    Atul 21
    Ram 101
    Rithwik 45
    saleem 55
    Amit 88
    Jancy 66
    Jibin 65
    Ashwin 75
    

    Expected Output

    After sorting
    Name ID
    Amit 88
    Ashwin 75
    Atul 21
    Jancy 66
    Jibin 65
    Ram 101
    Rithwik 45
    saleem 55

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.