Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# hey jkr how do i print 3 letters out in alphabitical order

Posted on 2004-09-29
Medium Priority
221 Views
#include <iostream>
using namespace std;

int main () {

string Letters;

Letter[0];
Letter[1];
Letter[2];

cout << "Please enter three characters:" << endl;
cin  >> Letter;

if Letter[0]

if ( Letter[0] == Letter[1] || Letter[1] == Letter[2] || Letter[0] == Letter[2])
cout << endl << "At least two characters are identical" << endl;

return 0;
}
0
Question by:tedschnieders
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 2
• 2

LVL 55

Expert Comment

ID: 12185941
Homework?
It is explained in the question you just have accepted. Using jkr's code:

#include <list>
#include <iostream>
using namespace std;

// some code to fill Letter[] array

list<char> lc;
list<char>::iterator it;

for ( int i = 0; i < 3; ++i)
lc.push_back(Letter[2] );

lc.sort();

for ( it = lc.begin(); it != lc.end(); ++it)
cout << *it;
cout << endl;

0

LVL 55

Expert Comment

ID: 12185947
Sorry, there is a typo:
lc.push_back(Letter[2] );
must be:
lc.push_back(Letter[i] );
0

LVL 55

Expert Comment

ID: 12185960
Obviously you have to put there the portion that fills the array, maybe you can integrate all the code you have received and post it for review.
0

LVL 55

Expert Comment

ID: 12185968
This portion is unnecesary:
Letter[0];
Letter[1];
Letter[2];
0

Author Comment

ID: 12185970
see thats just way to complex for me right now

can i use some kind of mask or if statement

sorry i am such a pain in the ass but i do learn alot from u guys

thanks
0

LVL 55

Accepted Solution

Jaime Olivares earned 2000 total points
ID: 12186037
Really this is the easiest way in C++, I can explain it step by step:

// Some "headers" needed by C++
#include <list>
#include <iostream>
using namespace std;

// Creates a "list" similar to an array, with sorting capabilities
list<char> lc;

// Creates an "iterator", an object that is used to transverse the list
list<char>::iterator it;

// This portion just insert three elements in the list (push_back method does)
for ( int i = 0; i < 3; ++i)
lc.push_back(Letter[2] );

// Here is the sortion method, couldn't be easier
lc.sort();

// Here ordered data is displayed using the iterator object instead a "for" loop
for ( it = lc.begin(); it != lc.end(); ++it)
cout << *it;

cout << endl;

If you want to make it with pure "if"s then will be look messy, for 3 element there are six ordering cases:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

then you have to make 6 "if"s, similar to (1st case):
if ( Letter[0] < Letter[1] && Letter[1] < Letter[2])
cout << Letter[0] << Letter[1] << Letter[2] << endl;

you will need 5 more blocks like this.

0

Author Comment

ID: 12186064
thank i appreciate it
0

LVL 13

Expert Comment

ID: 12187592
A small typo:
// This portion just insert three elements in the list (push_back method does)
for ( int i = 0; i < 3; ++i)
lc.push_back(Letter[2] ); // adds only last letter
should certainly be
// This portion just insert three elements in the list (push_back method does)
for ( int i = 0; i < 3; ++i)
lc.push_back(Letter[i] ); // adds all three letters
0

LVL 55

Expert Comment

ID: 12189148
Hey Steh, I have posted correction in my second comment!
0

LVL 13

Expert Comment

ID: 12189770
Sorry, overlooked that.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why tâ€¦
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more â€¦
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor anâ€¦
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month8 days, 7 hours left to enroll