Q:

C++ Program to Sort Elements in Lexicographical Order (Dictionary Order) Using For Loop

belongs to collection: Loop Programs In C ++Programming

0

Write C++ Program to Sort Elements in Lexicographical Order (Dictionary Order) Using For Loop

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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: 

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

total answers (1)

C++ Program To Find The GCD And LCM Of Two Numbers... >>
<< C++ Program To Print The Fibonacci Series Upto Giv...