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
#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';
}
Comments
Leave a comment