Programming with Passion

Make the best out of everything.

Friday 18 March 2016

Source Code to Sort Words in Dictionary Order

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

No comments:

Post a Comment