+ 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
17 Respuestas
+ 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.
+ 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.
+ 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
+ 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 (<, > ==)
+ 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?
+ 2
Shahroz Khan This question is not related to c++.
+ 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
+ 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.
+ 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).
+ 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
0
andriy kan pretty close there. how much the difference between 97(a) and 65(A) ? :)
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.
0
Answer is 32 bcs a(97) - A(65) = 32 !!!
compareTo will subtract the ASCII values.
0
ASCII difference
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 .
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
- 1
как написать код