Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>See when you check in your second code</p> <pre><code>if (last != c) { </code></pre> <p>You are not checking for all conditions.<code>last</code> could be equal to space, tab or new line. In all such cases it should not print new line. Lets call the set of these three special characters as <code>X</code>.</p> <p>Now when printing new line, you need to make sure that <code>last</code> character printed does not bring to set <code>X</code>. But you check that <code>last!=current</code>. Now current could be space, tab or new line. But it is only one value. It does not serve our need, our purpose.</p> <p>So instead replace it with</p> <pre><code> if (last != ' ' &amp;&amp; last != '\n' &amp;&amp; last != '\t' ) { </code></pre> <p>You can see the code here:</p> <pre><code>#include &lt;stdio.h&gt; #define IN 1 // inside a word #define OUT 0 // outside a word // program to print input one word per line, corrected bug if there was // more than one space between words to only print one \n int main(void) { int c, last, state; last = 0; // We need it to make sure that a newline is not printed in case first // char is space, tab or new line. state = OUT; while ((c = getchar()) != EOF) { if (c == ' ' || c == '\n' || c == '\t') { // if (last != c) if (last != ' ' &amp;&amp; last != '\n' &amp;&amp; last != '\t' &amp;&amp; last != 0 ) { state = OUT; printf("\n"); } } else if (state == OUT) { state = IN; } if (state == IN) { putchar(c); } last = c; } return 0; } </code></pre> <p><strong>Edit</strong><br/> Fixed the bug paxdiablo pointed out in comments.</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