Skip to content

Instantly share code, notes, and snippets.

@yorickdewid
Created October 12, 2014 18:30
Show Gist options
  • Save yorickdewid/0f123979a43f1abe501a to your computer and use it in GitHub Desktop.
Save yorickdewid/0f123979a43f1abe501a to your computer and use it in GitHub Desktop.
Example of lexicographical ordering in C
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]){
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment