Answer to Question #299254 in C++ for CHicken nuggets

Question #299254

Write a program that takes two strings and outputs the longest string. If they are the same length then output the second string.


Ex. If the input is:


almond pistachio

the output is:


pistachio


1
Expert's answer
2022-02-18T17:01:28-0500
#include <iostream>




std::string& compare_strings(std::string& str1, std::string& str2)
{
    if (str1.size() == str2.size())
    {
        return str2;
    }
    if (str1.size() > str2.size())
    {
        return str1;
    }
    return str2;
}


int main()
{
    std::string str1, str2;
    std::cout << "Enter string 1 :\n";
    std::cin >> str1;
    std::cout << "Enter string 2 :\n";
    std::cin >> str2;




    std::cout << compare_strings(str1, str2) << '\n';


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog