C Programming
Computer Programming

C program to Sort elements in the lexicographical order



Write a program in C to Sort elements in the lexicographical order (dictionary order)

\* C program to Sort elements in the lexicographical order *\

# include < stdio.h >
# include < conio.h >
int   main( )
{

char str[5][50], temp[50] ;
printf( "\n Enter 5 words: \n" ) ;
for(int i = 0; i < 5; ++i)
{
      fgets( str[i], sizeof(str[i]), stdin ) ;
}
for( int i = 0; i < 5; ++i )
{
      for( int j = i+1; j < 5 ; ++j )
      {
            if( strcmp(str[i], str[j]) > 0 )
            {
                  strcpy(temp, str[i]) ;
                  strcpy(str[i], str[j]) ;
                  strcpy(str[j], temp) ;
            }
      }
}

printf("\n In the lexicographical order: \n") ;
for( int i = 0; i < 5; ++i )
{
      fputs(str[i], stdout) ;
}
return 0 ;

}

Output of Program :

Output of C program to Sort elements in the lexicographical order