String compare | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

String compare

Hi Why below code results into -32? string s1={"Hello Sololearn"}; string s2={"Hello solo"}; cout << s1.compare(s2);

9th Jun 2020, 8:37 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Answers
+ 5
Vijay Raj Jain isn't S in s1 smaller than s in s2? That's why 83-115 =-32 is returned!
9th Jun 2020, 8:55 AM
Abhay
Abhay - avatar
0
0 : if both strings are equal. a value < 0 : if *this is shorter than str or, first character that doesn't match is smaller than str. a value > 0 : if *this is longer than str or, first character that doesn't match is greater Since s2<s1 so value returned is -32
9th Jun 2020, 8:50 AM
Vijay Raj Jain
0
So -5 may be 1. difference between number of characters difference in string 2. Difference between first mismatch character's ASCII value Is above right?
9th Jun 2020, 9:01 AM
Ketan Lalcheta
Ketan Lalcheta - avatar