Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ strings having a hard time getting this to work correctly Print the two stri

ID: 3669035 • Letter: C

Question

C++ strings having a hard time getting this to work correctly

Print the two strings in alphabetical order. Assume the strings are lowercase. End with newline. Sample output: capes rabbits

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

int main() {
string firstString;
string secondString;

firstString = "rabbits";
secondString = "capes";

if (secondString <= firstString ){
cout << "capes " << "rabbits" <<endl;}
else if (secondString >= firstString){
cout << "capes " << "rabbits" <<endl;}

  
return 0;
}

Testing input rabbits capes

Your output:

capes rabbits

Testing input capes rabbits

Your output:

capes rabbits

Testing input clearly clear

Expected output:

clear clearly

Your output:

capes rabbits

Tests aborted.

Explanation / Answer

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

/*
   Print the two strings in alphabetical order. Assume the strings are lowercase. End with newline. Sample output:
   capes rabbits
*/

int main() {
   string firstString;
   string secondString;

   firstString = "rabbits";
   secondString = "capes";

cout<< "Testing input"<< " " <<firstString << " " << secondString <<endl;

   /* Your solution goes here */
   if (firstString < secondString){
       cout << firstString << " " << secondString <<endl;
       //cout<< "output is correct";
   }
   else{
       cout << secondString << " " << firstString << endl;
   }

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote