+ 5

Why output is 32?

String size is different but output is coming 32 in both m and n. https://code.sololearn.com/cB92T8lChmOL/?ref=app

28th Feb 2020, 9:23 AM
A͢J
A͢J - avatar
17 Answers
+ 15
Becuse strings are compared in lexicographical order. In the 1st comparison: symbol 'A' has ASCII code 65, 'a' - 97, 'A' is less than 'a' (65<97), so string "AJ" is less then "aj", and the return value of compareTo() must be greater than 0 for that comparison (value may be any (like 32), but it sholud be >0). 2nd comparison is the same.
28th Feb 2020, 9:33 AM
andriy kan
andriy kan - avatar
+ 6
str1.compareTo(str2) 1) return a positive value if str1>str2. 2) return a negative value if str1<str2. 3) return 0 if str1==str2. These values are returned based on their lexicographic order. Anyways a-A=97-65=32.
28th Feb 2020, 9:48 AM
Avinesh
Avinesh - avatar
+ 5
Infinity 19 when case sensitive thing is came across to the CompareTo() method than it will behave like that as in ASCII code or Unicode values difference between small and capital letters are 32. https://teamtreehouse.com/community/why-does-applecomparetoapple-return-32-and-not-1-0-or-1
28th Feb 2020, 10:09 AM
DishaAhuja
DishaAhuja - avatar
+ 3
@Taste Ofcourse it is 32 (difference between lowcase and uppercase letters in ASCII), and it is the result of the internal(!) algorithm (becuse it is simpler to return current value of the register that contains the difference between comparing symbols if it is not euqal to 0). And that value is also useless (because index(position) of that difference is not returned). Result of comparedTo() should be compared to 0 (<, > ==)
28th Feb 2020, 10:16 AM
andriy kan
andriy kan - avatar
+ 2
Avinesh and DishaAhuja I got that the difference between Lower Letter and Capital Letter is either +32 or -32 but how compareTo method compare String, Character by Character? And why the difference between these two string :- String str1 = "AJ"; String str2 = "AJA"; Is 1?
28th Feb 2020, 10:51 AM
A͢J
A͢J - avatar
+ 2
Shahroz Khan This question is not related to c++.
29th Feb 2020, 6:14 PM
A͢J
A͢J - avatar
+ 2
The java string compareTo() method compares the given string with current string lexicographically. It's is returning answer as '32' by comparing the ASCII values We know that, A = '65' a = '97' 'A' ASCII value is smaller than the ASCII value of 'a'... So, By lexicographic order, a - A = 97-65=32
29th Feb 2020, 8:15 PM
Aryan Karumuri
Aryan Karumuri - avatar
+ 2
This post for those who wrote that the compareTo returns the difference between the characters. Please answer the question what the following comparisons will return: "AAAA".compareTo("A") = ? "DDD".compareTo("") = ? Answer: 3 It is the difference between the lengths of the strings, but not the difference between the characters!!! The function internal code is like this: public int compareTo(String s) { int minLength = this.length(); if(s.length() < minLength) minLength = s.length(); for(int i = 0; i < minLength; ++i){ int d = this.charAt(i) - s.charAt(i); if(d != 0) return d; } return this.length() - s.length(); } But this is internal code and it can be changed. Then the returned numerical value may be other. Also the return value does not provide any information on how much strings are differ. All of the following comparisons will return 3: "DDD".compareTo("") "DDD".compareTo("A") "DDD".compareTo("DA") "DDD".compareTo("DDA") "NOP".compareTo("KLM") Only following is guaranteed: return value is negative if str1<str2, is positive if str1>str2, is zero if str1==str2 and you should think of compareTo return value only as positive, negative or zero number and don't evaluate how big or small it is.
29th Feb 2020, 9:27 PM
andriy kan
andriy kan - avatar
+ 1
@Infinity 19 Because if there is no index position at which strings differ, then compareTo returns the difference of the lengths of the strings. But again, that is result of internal algorithm. You should compare the result of compareTo() to 0, and do not consider what that value is (only positive, negative, or eual to 0).
28th Feb 2020, 10:58 AM
andriy kan
andriy kan - avatar
+ 1
Infinity 19 as andriy said, compareTo() will search for first mismatch character and when it found that then ascii difference is return. In your case as the "AJ" and "AJA" is compared so it will return -1 as second string has greater ASCII values
28th Feb 2020, 11:02 AM
DishaAhuja
DishaAhuja - avatar
0
andriy kan pretty close there. how much the difference between 97(a) and 65(A) ? :)
28th Feb 2020, 9:57 AM
Taste
Taste - avatar
0
String str1 = "AJ"; String str2 = "AJA"; str1[0] == str2[0] // A str1[1] == str2[1] // J str1 has no element with index 2, but str2 has, and all preceding symbols are equal, so lengthes of string are compared length of str2 = 3, length of str1 = 2, so 3 - 2 = 1 But don't consider that values. Think of them as positive, negative or zero.
28th Feb 2020, 11:09 AM
andriy kan
andriy kan - avatar
0
Answer is 32 bcs a(97) - A(65) = 32 !!! compareTo will subtract the ASCII values.
29th Feb 2020, 9:53 AM
Ravi Prakash Dixit
Ravi Prakash Dixit - avatar
0
ASCII difference
29th Feb 2020, 5:54 PM
tauseef ansari
tauseef ansari - avatar
0
Because compareTo() method returns the difference of the value of Unicode characters of the respective strings it compares.In Unicode lowercase and uppercase chars has a difference of 32 . For ignoring the case sensitiveness you can use compareToIgnoreCase() method .It will return zero in your case .
29th Feb 2020, 6:50 PM
Rontu Barhoi
Rontu Barhoi - avatar
0
 this is how compareTo works: If both the strings are equal then this method returns 0 else it returns positive or negative value. The result is positive if the first string is lexicographically greater than the second string else the result would be negative
1st Mar 2020, 3:36 AM
Khaled ^^ خالد القريشي‎
Khaled ^^ خالد القريشي‎ - avatar
- 1
как написать код
29th Feb 2020, 8:53 PM
Fanbro 228
Fanbro 228 - avatar