Program to Sort Elements in the Lexicographical order in CPP (C plus plus)

Program to Sort Elements in the Lexicographical order in CPP (C plus plus)

In this tutorial, we will learn about Program to sort elements in the Lexicographical order in CPP (C plus plus).

#include <iostream>

using namespace std;

int main()
{    
	string tmp;
	string val[10];

    cout<<"Please enter any ten words of your choice: "<<endl;
    
    for(int i=0;i<10;++i)
    {
      getline(cin, val[i]);
    }

    for(int i = 0; i < 9; ++i)
    
       for( int j = i+1; j < 10; ++j)
       {
          if(val[i] > val[j])
          {
            tmp = val[i];
            val[i] = val[j];
            val[j] = tmp;
          }
    }

    cout<<"Entered words in lexicographical order: "<<endl;

    for(int i=0;i<10;++i)
    {
       cout << val[i] << endl;
    }
    return 0;
}

Output:

Program to Sort Elements in the Lexicographical order in CPP (C plus plus)
Program to Sort Elements in the Lexicographical order in CPP (C plus plus)
Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.