Problem statement
Write a program to take words or elements as input from user and sort it as Lexicographical/Dictionary order.
What is meant by Lexicographical Order:
Lexicographical Order is also known as lexical order, dictionary order, alphabetical order, lexicographic order.
Lexicographic order is the way of ordering of words based on the alphabetical order of their component letters.
It is similar to the way in we search any word in the dictionary.
Example:
Suppose following numbers: Ram, Rita, Abhinav, Abhish, Rina, Josh.
Now Lexicographical Order is- Abhinav, Abhish, Josh, Ram, Rina, Rita.
Program to Sort Strings in Lexicographical/Dictionary Order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include<stdio.h> #include<string.h> int main() { int i,j; char str[10][50]={"ram narayan","sita vishnupanth","rita","raj kumar","lakshman sheth","rina rao"}; char temp[50]; //sorting elements in order 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); } } } //printing sorted elements printf("In lexicographical order: \n"); for(i=0;i<10;++i) { printf(str[i]); printf("\n"); } } |
Output:
1 2 3 4 5 6 7 8 |
In lexicographical order: lakshman sheth raj kumar ram narayan rina rao rita sita vishnupanth |