C++ Program to Sort Strings Lexicographically

In this program, the user is ask to enter the several strings, then the given strings will be sorted lexicographically.

With strings, the usual order is Lexicographic Order. This is dictionary order, except that all the uppercase letters precede all the lowercase letters. ... Two strings are lexicographically equal if they are the same length and contain the same characters in the same positions.

For example, you have a strings as HTML, C, PHP then the sorted order is C, HTML, PHP.



Source Code
#include<iostream>
#include<string.h>
using namespace std;

int main()
{
    int i, j;
    char temp[30];
    char str[10][30];

    cout<<"Enter any 10 words\n";
    for(i=0; i<10; i++)
    {
        gets(str[i]);
    }

    for(i=0; i<10; i++)
    {
        for(j=0; j<10; j++)
        {
            if(strcmp(str[j], str[j+1]) > 0)
            {
                strcpy(temp, str[j]);
                strcpy(str[j], str[j+1]);
                strcpy(str[j+1], temp);
            }
        }
    }

    cout<<"\nSorted words are..\n";
    for(i=0; i<10; i++)
    {
        cout<< str[i]<< endl;
    }
    return 0;
}
Output
Enter any 10 words
C
C++
Java
PHP
Perl
Html
VB
JSP
Python
Android

Sorted words are..
Android
C
C++
Html
JSP
Java
PHP
Perl
Python
VB