0
Can you compare 2 strings (the strings contain the same characters) and get 0 as the value of their comparation?
4 Answers
+ 4
int main()
{
string string1 = "test";
string string2 = "test";
int n = ( string1 == string2 ) ? 0 : 1;
cout << n << endl;
return 0;
}
+ 3
you can make your own function, that if they are equals, return 0. else -1 , or wv.
0
#include <iostream>
#include <string>
int main ()
{
std::string str1 ("green apple");
std::string str2 ("red apple");
if (str1.compare(str2) != 0)
std::cout << str1 << " is not " << str2 << '\n';
if (str1.compare(6,5,"apple") == 0)
std::cout << "still, " << str1 << " is an apple\n";
return 0;
}
0
Thank you to all of you