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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.