Canna

Tuesday, 25 September 2018

Sorting Names

  • Problem Description

    Professor Manvi has to take attendance for her class, but the names are scattered. She has to sort the names in alphabetical order. Write a code to perform sorting
  • Test Case 1

    Input (stdin)
    4
    
    suba
    
    bala
    
    deepa 
    
    arun
    
    
    Expected Output
    arun
    
    bala
    
    deepa
    
    suba
  • Test Case 2

    Input (stdin)
    4
    
    akashi
    
    jero
    
    mani
    
    parkavi
    
    
    Expected Output
    akashi
    
    jero
    
    mani
    
    parkavi
  • Program
  • #include <stdio.h>
    #include <string.h>
    int main()
    {
      int i,j,num;
      char str[50][50],temp[25];
      scanf("%d",&num);
      for(i=0;i<num;i++)
      {
        scanf("%s",str[i]);
      }
      for(i=0;i<num;i++)
      {
        for(j=i+1;j<num;j++)
        {
          if(strcmp(str[i],str[j])>0)
             {
               strcpy(temp,str[i]);
               strcpy(str[i],str[j]);
               strcpy(str[j],temp);
             }
          }
      }
             for(i=0;i<num;i++)
             {
             printf("\n%s",str[i]);
             }
     return 0;
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.