hey guys..ok so i did this problem about 80% but i cnt figure out the last part which is sort the characters in order of increasing size.
Question:
Input: 4 words (strings with no spaces) and the order in which they are to be displayed, forward alphabetical, reverse alphabetical or in order of increasing size.
Output: the words in their selected order.
Sample run 1:
Please enter 4 words: wall table answer set
In what order would you like to display the words?
a: alphabetical order
r: reverse alphabetical order
s: in order of increasing size
Enter a, r or s: s
1) set
2) wall
3) table
4) answer
Sample run 2:
Please enter 4 words: wall table answer set
In what order would you like to display the words?
a: alphabetical order
r: reverse alphabetical order
s: in order of increasing size
Enter a, r or s: r
1) wall
2) table
3) set
4) answer
No arrays, programmer defined functions or loops. You may use if statements and/or switch statements in your code. Also, you may use the built-in swap() function
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main ()
{
const int SIZE = 40;
char first[SIZE], second[SIZE], third[SIZE], fourth[SIZE], order;
cout<<"Please enter four words: ";
cin>>first>>second>>third>>fourth;
cout << "In what order would you like to display the words?"<<endl;
cout << "a: alphabetical order"<<endl;
cout << "r: reverse alphabetical order"<<endl;
cout << "s: in order of increasing size"<< endl;
cout << "Enter a, r or s: ";
cin >> order;
/*if (order == 'a')
if (strcmp (first,second)<0 && strcmp (first,third)<0 && strcmp(first, fourth)<0)
// if (strcmp (first, third)<0)
cout<< first;
else cout<<"yo"<<endl;*/
string f = first, s = second, t = third, fo = fourth;
if (order == 'a')
{
if (f>s)
swap (f,s);
if (s>t)
swap (s,t);
if (f>s)
swap(f,s);
if (f>fo)
swap (f,fo);
if (s>fo)
swap(s,fo);
if (t>fo)
swap (t,fo);
cout<<"1) "<<f<<endl;
cout<<"2) "<<s <<endl;
cout<<"3) "<<t<<endl;
cout<< "5) "<<fo<<endl;
}
else if (order == 'r')
{
if (f>s)
swap (f,s);
if (s>t)
swap (s,t);
if (f>s)
swap(f,s);
if (f>fo)
swap (f,fo);
if (s>fo)
swap(s,fo);
if (t>fo)
swap (t,fo);
cout<<"1) "<<fo<<endl;
cout<<"2) "<<t <<endl;
cout<<"3) "<<s<<endl;
cout<< "5) "<<f<<endl;
}
return 0;
}