sorting strings

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 3 Exercise 7
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 3 Exercise 7

#include "std_lib_facilities.h"

int main()
{
	string word1 = " ", word2 = " ", word3 = " ";
	string smallest = " ", medium = " ", largest = " ";
	cout << "enter word 1, word 2, word 3 devided by space: ";
	cin >> word1 >> word2 >> word3;

	cout << "you entered three words: " << "word1: " << word1 << ", word2: " << word2 << ", word3: " << word3 << "\n";

	if (word1 <= word2 && word1 <= word3)
	{
		smallest = word1;
		if (word2 <= word3)
		{
			medium = word2;
			largest = word3;
		}
		else
		{
			medium = word3;
			largest = word2;
		}
	}
		
	if(word2 <= word1 && word2 <= word3)
	{
		smallest = word2;
		if (word1 <= word3)
		{
			medium = word1;
			largest = word3;
		}
		else
		{
			medium = word3;
			largest = word1;
		}

	}

	if(word3 <= word2 && word3 <= word1)
	{
		smallest = word3;
		if (word1 <= word3)
		{
			medium = word1;
			largest = word2;
		}
		else
		{
			medium = word2;
			largest = word1;
		}
	}

	cout << smallest << ", " << medium << ", " << largest << "\n";

	keep_window_open();
}
Output:
enter word 1, word 2, word 3 devided by space: how are you
you entered three words: word1: how, word2: are, word3: you
are, how, you
Please enter a character to exit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.