Question

Today we are practicing our ABC's. Write a program that will accept three separate names as...

Today we are practicing our ABC's. Write a program that will accept three separate names as inputs (ex: "billy"). Then display the names in alphabetical order. The names will always use all lowercase letters. For example: given the input

"lauren" 
"rodger"
"bob"

The output should be

"bob"
"lauren"
"rodger"

Note: Think carefully about the logic needed to solve this problem and remember there are only three names that need to be sorted.

P.S. The computer language we are using is C++

Homework Answers

Answer #1

#include <iostream>
using namespace std;

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

cout << "Enter 3 strings : " << endl;

for(int i = 0; i < 3; i++) // taking input from user
cin >> str[i];

for(int i = 0; i < 2; i++) // sorting the strings in alphabetical order
for( int j = i+1; j < 3; j++)
{
if(str[i] > str[j]) // comparing two strings
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}

cout << endl << "Names in alphabetical order : " << endl;

for(int i = 0; i < 3; i++) // displaying sorted strings
cout << str[i] << endl;

return 0;
}

Output :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT