28/03/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

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: