ut/cin.getline() for the third string similar to the ones for first and second strings. 3. Add the appropriate case to the "if/else if" structure such that the three strings will be displayed in alphabetical order. Hint: There are 6 possible cases (3!, 3 factorial = 3*2*1 = 6) for all complete permutations. The easiest check is to input the three strings as single letters like: "a", "b", and "c". You "if/else if" should cover 6 cases for the these possible input order: abc, acb, bac, bca, cab, cba. Regardless of the input order the output should be always abc. First "if" condition check is like: if ( strcmp(name1,name2) < 0 && strcmp(name2,nam
In C++ with comments please
Modify the program 12-7 from page 826 as follows:
1. Add a third C-string as name3[NAME_LENGHT].
2. Add a pair of cout/cin.getline() for the third string similar to the ones for first and second strings.
3. Add the appropriate case to the "if/else if" structure such that the three strings will be displayed in alphabetical order.
Hint: There are 6 possible cases (3!, 3 factorial = 3*2*1 = 6) for all complete permutations. The easiest check is to input the three strings as single letters like: "a", "b", and "c". You "if/else if" should cover 6 cases for the these possible input order: abc, acb, bac, bca, cab, cba. Regardless of the input order the output should be always abc.
First "if" condition check is like:
if ( strcmp(name1,name2) < 0 && strcmp(name2,name3) < 0 )
cout << name1 << " " << name2 << " " << name3 << endl;
You have to complete the rest of the 5 "else if" condition. The last "else" can be used to display the message that all three strings are the same.
Save and submit you program as pr12-7_Lab.cpp.
pr12-7:
// This program uses the return value of strcmp to
// alphabetically sort two strings entered by the user.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
// Two arrays to hold two strings
const int NAME_LENGTH = 30;
char name1[NAME_LENGTH], name2[NAME_LENGTH];
// Read two strings
cout << "Enter a name (last name first): ";
cin.getline(name1, NAME_LENGTH);
cout << "Enter another name: ";
cin.getline(name2, NAME_LENGTH);
// Print the two strings in alphabetical order
cout << "Here are the names sorted alphabetically:\n";
if (strcmp(name1, name2) < 0)
cout << name1 << endl << name2 << endl;
else if (strcmp(name1, name2) > 0)
cout << name2 << endl << name1 << endl;
else
cout << "You entered the same name twice!\n";
return 0;
}
Step by step
Solved in 2 steps with 1 images