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

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  char word[12][50],temp[50];
  int i,j;
  printf("Enter 12 words:\n");
  for(i=0;i<12;++i)
    gets(word[i]);
  for(i=0;i<11;i++)
    for(j=i+1;j<12;j++)
    {
      if(strcmp(word[i],word[j])>0)
        {
          strcpy(temp,word[i]);
          strcpy(word[i],word[j]);
          strcpy(word[j],temp);
        }
    }
  printf("\nIn lexicographical order: \n");
  for(i=0;i<12;++i)
    puts(word[i]);
  getch();
}


No comments:

Post a Comment