Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The line</p> <pre><code>int j = liczba.charAt(i) - 47; </code></pre> <p>Subtracts the character code of (i+1)th character <code>liczba</code> by 47. Refering to an <a href="http://www.asciitable.com/" rel="nofollow">ASCII table</a>, 47 maps to "/", whose ASCII code is one less than "0". </p> <p>Note that I am assuming the following, as your code appears to be in Java.</p> <ul> <li>String indexes starts at zero i.e. first character has index 0, second character has index 1, and so on </li> <li>It seems that characters and integers can be used interchangeably, because a character is internally represented by an integer, namely, the ASCII code of the character.</li> <li>That said, the <code>char</code> type is actually an integer type, with less range</li> </ul> <p>Hence, this code is to turn characters "0" to the integer 1, "1" to the integer 2, etc. </p> <p>For example, when the 1st character (<code>liczba.charAt(0)</code>) is the character <code>0</code>, <code>liczba.charAt(0)</code> returns character <code>'0'</code>, which is also equals to number <code>48</code> -- because <code>'0'</code> has an ASCII code of 48. </p> <p>Subtracts 48 with 47 gets 1, so it would convert character '<code>0</code>' to integer 1. </p> <p><strong>However</strong> it seems that this code could cause array index out of bounds error (assuming zero-based array indexes). when a digit is <code>'9'</code>, this line returns 10. This would cause an aforementioned error. Unless this code's language's array are 1-based. However, even in this case, this line</p> <pre><code>int count = digits[0]; </code></pre> <p>would simply fail. This code seems to fail with the common <a href="http://en.wikipedia.org/wiki/Off_by_one_error" rel="nofollow">Off by one error</a></p> <p>I believe this line should actually read </p> <pre><code>int j = liczba.charAt(i) - 48; </code></pre> <p>so that character <code>'0'</code> is converted to number 0.</p> <p>If this still disturb you, you may change this line to</p> <pre><code>int j = liczba.charAt(i) - '0'; </code></pre> <p>so it would be clearer. Subtract the code of <code>'1'</code> and the code of <code>'0'</code> gets you the integer 1, clear enough :) </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload