Write C++ Program to Sort Elements in Lexicographical Order (Dictionary Order) Using For Loop
#include<iostream> #include <cstring> using namespace std; int main() { int i,j,n; char str[10][50],temp[50]; cout << "Enter The No. Of Words: " << endl; cin>>n; for(i=0;i<n;++i) cin.getline(str[i], 50); for(i=0;i<n-1;++i) for(j=i+1;j<n ;++j) { if(strcmp(str[i],str[j])>0) { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } } cout << "In lexicographical order: " << endl; for(i=0;i<n;++i){ cout << str[i] << endl; } return 0; }
Output:
Enter The No. Of Words:
10
nerdutella
example
c++
programming
mcv
love
niit t
month
year
In lexicographical order:
nit
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Output:
Enter The No. Of Words:
10
nerdutella
example
c++
programming
mcv
love
niit t
month
year
In lexicographical order:
c++
example
love
mcv
month
nerdutella
nit
programming
year
need an explanation for this answer? contact us directly to get an explanation for this answer