C Program To Sort An Array Of Names In Alphabetical And Reverse Order

How to write a C Program To Sort An Array Of Names In Alphabetical And Reverse Order in C Programming Language ?


Solution For C Program :

/*C Program To Sort An Array Of Names In Alphabetical And Reverse Order.*/

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
    {
    int i,j,k;
    char name[10][10],tname[10][10],temp[10];
    clrscr();
    printf("How many numbers you want to store:=");
    scanf("%d",&k);
    printf("Enter numbers for in an array:=");
       fflush(stdin);

    for(i=0;i<k;i++)
       {
       scanf("%s",name[i]);
       strcpy(tname[i],name[i]);
       }

    for(i=1;i<k;i++)
       {
       for(j=1;j<k;j++)
          {
          if(strcmpi(name[j],name[j-1])<0)
          {
          strcpy(temp,name[j-1]);
          strcpy(name[j-1],name[j]);
          strcpy(name[j],temp);
          }
          }
       }
    printf("The sorted names in alphabetical order [A to Z Format] are:\n");
    printf("\tOldlist\t\tNew list\n\n");
    for(i=0;i<k;i++)
        {
        printf("\t%s\t\t%s\n",tname[i],name[i]);
        }
    printf("\n\nThe sorted names in [Z to A Format] are:\n");
    for(i=k-1;i>=0;i--)
        {
        printf("\t%s\n",name[i]);
        }
    getch();
    }


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable




Learn More :