Sort Elements in Lexicographical Order (Dictionary Order)
Course- C >
This program takes 10 words from user and sorts elements in lexicographical order. To perform this task, two dimensional string is used.
Source Code to Sort Words in Dictionary Order
#include<stdio.h>
#include <string.h>
int main(){
int i,j;
char str[10][50],temp[50];
printf("Enter 10 words:\n");
for(i=0;i<10;++i)
gets(str[i]);
for(i=0;i<9;++i)
for(j=i+1;j<10 ;++j){
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("In lexicographical order: \n");
for(i=0;i<10;++i){
puts(str[i]);
}
return 0;
}
Output
Enter 10 words: fortran java perl python php javascript c cpp ruby csharp In lexicographical order: c cpp csharp fortran java javascript perl php python ruby