Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>for (i = 0; str1[i] &amp;&amp; str2[i]; i++) </code></pre> <p>is the same as</p> <pre><code>for (i = 0; (str1[i] != 0) &amp;&amp; (str2[i] != 0); i++) </code></pre> <p>which is the same as</p> <pre><code>for (i = 0; (str1[i] != '\0') &amp;&amp; (str2[i] != '\0'; i++) </code></pre> <p>Basically if any expression is used in a conditional statement, then the value is checked for boolean - true or false. If it's not a boolean - say it's an integral type, then 0 is <code>false</code> anything else is <code>true</code>.</p> <p>Here <code>str[i]</code> is a <code>char</code> - which is an integral type. So if str[i] is 0, then it evaluates to false, else it evaluates to true.</p> <p>For eg.</p> <pre><code>char c = 'A'; if(c) </code></pre> <p>is the same as</p> <pre><code>if (c != 0) </code></pre> <p>which is the same as</p> <pre><code>if (c != '\0') </code></pre> <p>c is 'A' (which in ascii is 65). 65 != 0, hence it evaluates to true &amp; the <code>if</code> will be entered.</p> <p>if instead, you have</p> <pre><code>char c = 0; </code></pre> <p>or</p> <pre><code>char c = '\0'; </code></pre> <p>then c evaluates to 0. Hence if(c) evaluates to false &amp; the if is not entered.</p> <p>You can extend the same logic to str[i] which is an <code>char</code>.</p> <p>If you have <code>str1[]="rate"</code>, it's the same as <code>str1[0] = 'r', str1[1] = 'a', str1[2] = 't', str1[3] = 'e', str1[4] = 0</code>.</p> <p>About the <code>count1[str1[i]]++;</code></p> <p>It's a count of how many times each character occurs - for eg. if the char set is ascii, then at the end of string traversal, count['A'] (which is the same as count[65]) will contain the number of times 'A' occurred in the string.</p> <p>It will work only if each member of the count arrays are initialized to <code>0</code> somewhere (or they are global).</p> <p>consider </p> <pre><code>str1[] = "ABAB"; </code></pre> <p><code>count[str1[0]]</code> is same as <code>count['A']</code> which is same as <code>count[65]</code> (if char set is ascii).</p> <p>The <code>++</code> will cause count['A'] to become 1</p> <p>When <code>i</code> becomes 1, <code>count[str1[1]]++</code> causes <code>count['B']</code> to become 1. <code>i</code> = 2, then <code>count['A']</code> becomes 2. <code>i</code> = 3, then <code>count['B']</code> becomes 2.</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