Write a C++ Program to Sort Elements in Lexicographical Order (Dictionary Order)

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

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn C++ programming concepts. You can learn C++ language at your own speed and time. One can learn concepts of C++ language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

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

Write a C++ Program to Sort Elements in Lexicographical Order (Dictionary Order)

PROGRAM:C++ Program to Sort Elements in Lexicographical Order (Dictionary Order)

/* C++ Program to Sort Elements in Lexicographical Order (Dictionary Order) */

#include <iostream>
using namespace std;

int main()
{
string str[10], temp;

cout << “Enter 10 words: ” << endl;
for(int i = 0; i < 10; ++i)
{
getline(cin, str[i]);
}

// Use Bubble Sort to arrange words
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9 – i; ++j) {
if (str[j] > str[j + 1]) {
temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}

cout << “In lexicographical order: ” << endl;

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

Leave a Reply

Your email address will not be published. Required fields are marked *