Solution:
As far as your problem is concern, yes! there are some other ways to compare two strings in c++. We can use Relational operators and std:: Compare() function. Hence I love the second option, I am going with it.
#include <iostream>
using namespace std;
void compareString(string str1, string str2){
int n = str1.compare(str2);
if (n != 0)
cout << str1 << " is not as like as "
<< str2 << endl;
if (n > 0)
cout << str1 << " is larger than "
<< str2 << endl;
else
cout << str2 << " is larger than "
<< str1 << endl;
}
int main(){
string str1("Hello-World");
string str2("World-Hi");
compareString(str1, str2);
return 0;
}
Here I declared two different string into two differents variable str1 and str2. And then pass these string through a function compareString() as parameters. The program should throw an output
Hello-World is not as like as World-Hi
World-Hi is larger than Hello-World
Leave me a kudos if the problem is solved. Thanks!