Canna

Tuesday, 25 September 2018

Sorting of strings

  • Problem Description

    Program to Sort Strings in Dictionary Order
  • Test Case 1

    Input (stdin)
    3
    
    hello
    
    apple
    
    james
    
    
    Expected Output
    apple
    
    hello
    
    james
  • Test Case 2

    Input (stdin)
    5
    
    zen
    
    yatch
    
    app
    
    james
    
    dare
    
    
    Expected Output
    app
    
    dare
    
    james
    
    yatch
    
    zen
  • Program
  • #include<stdio.h>
    #include<string.h>
    int main()
    {
      int i,j,n;
       char str[10][50],temp[50];
      scanf("%d",&n);
      for(i=0; i<n; ++i)
         scanf("%s[^\n]",str[i]);
     for(i=0; i<n-1; ++i)
            for(j=i+1;j<n;++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<n; ++i)
        {
            printf("%s\n",str[i]);
        }
      return 0;
    }

No comments:

Post a Comment

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