String compare method, what is the return value telling us? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

String compare method, what is the return value telling us?

string s1("hi sololearn"); string s1("hi solo"); cout << s1.compare(s2); I am sure I saw something about this in the C++ lessons but I can't find it now. I did a challenge with the code above. The answer is 5. string::compare returns an int. 0 if the strings are equal, an int < 0 if s1 is shorter than s2 or an int > 0 if s1 is longer than s2. So I understand that it is telling me that s1 is 5 characters longer than s2. Is that all that it tells me? It is only telling me that s1 contains s2 and that s1 lexicographically comes before s2? Is there anything else we can know about the two strings from the info we have?

30th Mar 2019, 9:14 PM
Nathan Stanley
Nathan Stanley - avatar
1 Answer
+ 1
~ swim ~ when you say "mismatch" does this mean the letter L after "solo" in the string s1? It doesn't matter what char it is, because any char will be > no char according to C++ right? If the returned value is compiler specific, isn't this a bit ambiguous for a challenge question? I think there was an option for the answer to be 1.
30th Mar 2019, 11:41 PM
Nathan Stanley
Nathan Stanley - avatar