Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is not an assignment, but is an equality test:</p> <pre><code>s1[arraycntr] == '\t'; </code></pre> <p>you meant:</p> <pre><code>s1[arraycntr] = '\t'; </code></pre> <p>If you compile with a high warning level, the compiler may emit a message alerting you to this. The Microsoft VC compiler emits the following warning:</p> <blockquote> <p>C:\devel\cpp\stackoverflow\main.c(32) : warning C4553: '==' : operator has no effect; did you intend '='?</p> </blockquote> <p>The initial <code>for</code> loops only check if <code>s1</code> and <code>s2</code> have the same values in the same indexes, it does not check if a char in <code>s1</code> exists anywhere in <code>s2</code>. The terminating conditions on the <code>for</code> loops should also be the lengths of <code>s1</code> and <code>s2</code>, not <code>MAXLTR</code>:</p> <pre><code>size_t arraycntr,comparecntr; for(comparecntr = 0; comparecntr &lt; strlen(s2); comparecntr++) { for(arraycntr = 0;arraycntr &lt; strlen(s1) ;arraycntr++) { if(s1[arraycntr] == s2[comparecntr]) s1[arraycntr] = `\t`; } } </code></pre> <p>The next <code>for</code> loop should use <code>strlen(s1)</code> also and just assign null terminator to <code>temp</code> after:</p> <pre><code>comparecntr = 0; for(arraycntr = 0; arraycntr &lt; strlen(s1); arraycntr++) { if(s1[arraycntr] != `\t`) { temp[comparecntr] = s1[arraycntr]; ++comparecntr; } } temp[comparecntr] = '\0'; </code></pre> <p><code>temp</code> is not initialised anywhere, so contains random data, apart from that just entered during this <code>for</code>. Without a null terminator in <code>temp</code>, <code>s1</code> will end with no null terminator also (you will probably see garbage printed afterwards). Finally, just <code>strlen(temp) + 1</code> when populating <code>s1</code>:</p> <pre><code>for(arraycntr = 0;arraycntr &lt; strlen(temp) + 1; arraycntr++) s1[arraycntr] = temp[arraycntr]; </code></pre> <p>The <code>+ 1</code> will copy the null terminator to <code>s1</code>.</p> <p>Minor note, instead of calling <code>strlen()</code> in the terminating condition of the <code>for</code> loops you can store this instead:</p> <pre><code>size_t chars_to_copy; for(arraycntr = 0, chars_to_copy = strlen(temp) + 1; arraycntr &lt; chars_to_copy; arraycntr++) { s1[arraycntr] = temp[arraycntr]; } </code></pre>
 

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