C Program to Sort Elements in Lexicographical Order (Dictionary Order)

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[6][50]={"ram bahadur","lila maya","rupa","raj kumar","naba raj","deep raj"};
  int i,j;
  char temp[50];
  for(i=0;i<5;i++)
    {
    for(j=i+1;j<6;j++)
      {
        if(strcmp(name[i],name[j])>0)
          {
            strcpy(temp,name[i]);
            strcpy(name[i],name[j]);
            strcpy(name[j],temp);
          }
      }
    }
  printf("In lexicographical order: \n");
  for(i=0;i<6;++i)
    {
    puts(name[i]);
    }
  getch();
}

OUTPUT:

No comments:

Post a Comment