0

Can you compare 2 strings (the strings contain the same characters) and get 0 as the value of their comparation?

8th Jan 2017, 8:50 AM
Dinu
Dinu - avatar
4 Respuestas
+ 4
int main() { string string1 = "test"; string string2 = "test"; int n = ( string1 == string2 ) ? 0 : 1; cout << n << endl; return 0; }
8th Jan 2017, 9:03 AM
visph
visph - avatar
+ 3
you can make your own function, that if they are equals, return 0. else -1 , or wv.
8th Jan 2017, 9:03 AM
Nahuel
Nahuel - avatar
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; }
8th Jan 2017, 9:23 AM
ASNM
ASNM - avatar
0
Thank you to all of you
8th Jan 2017, 9:25 AM
Dinu
Dinu - avatar