fastread homefastrread library fastread menu

C++ : Sort Elements in Lexicographical Order (Dictionary Order)

Tutorial by:Manisha Dubey      Date: 2016-06-18 05:11:04

❰ Previous Next ❱

This program takes 10 words from user and sorts elements in lexicographical order. To perform this task, two dimensional array is used.

Source Code to Sort Words in Dictionary Order


#include<iostream>
#include <cstring>
using namespace std;
int main(){
    int i,j;
    char str[10][50],temp[50];
    cout << "Enter 10 words: " << endl;
    for(i=0;i<10;++i)
        cin.getline(str[i], 50);
    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);
          }
    }
    cout << "In lexicographical order: " << endl;
    for(i=0;i<10;++i){
       cout << str[i] << endl;
    }
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

❰ Previous Next ❱


C++

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website