Monday, 28 March 2016

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

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

www.programmingwithbasics.com

No comments:
Write comments

Recommended Posts × +