Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1><s>C++</s> C, <s>411</s> <s>400</s> <s>388</s> 367 characters</h1> <p>My first attempt ever at a code golf.</p> <pre><code>#include&lt;stdio.h&gt; main(){char m[999],w[99];int r,c,l=-1,x=-1,y=-1,i=0,j,k;scanf("%s %d %d" ,w,&amp;r,&amp;c);for(;w[l+1];++l);for(;i&lt;r*c;scanf("%s",&amp;m[i++]));for(;y&lt;2;++y) for(;x&lt;2;++x)if(x|y)for(i=(y&lt;0)*l;i&lt;r-(y&gt;0)*l;++i)for(j=(x&lt;0)*l;j&lt;c-(x&gt;0 )*l;++j)for(k=0;k&lt;=l&amp;&amp;w[k++]==m[(i+k*y)*c+j+k*x];)k&gt;l?printf( "row %i, column %i --&gt; row %i, column %i\n",i+1,j+1,i+y*l+1,j+x*l+1):0;} </code></pre> <p>It compiles without warnings on gcc with no additional flags.</p> <p>Here's the version with whitespace:</p> <pre><code>#include&lt;stdio.h&gt; main() { char m[999], w[99]; int r, c, l = -1, x = -1, y = -1, i = 0, j, k; scanf("%s %d %d", w, &amp;r, &amp;c); for (; w[l+1]; ++l); for (; i &lt; r*c; scanf("%s", &amp;m[i++])); for (; y &lt; 2; ++y) for (; x &lt; 2; ++x) if (x | y) for (i = (y&lt;0) * l; i &lt; r - (y&gt;0) * l; ++i) for (j = (x&lt;0) * l; j &lt; c - (x&gt;0) * l; ++j) for (k = 0; k &lt;= l &amp;&amp; w[k++] == m[(i+k*y) * c + j+k*x];) k &gt; l ? printf("row %i, column %i --&gt; row %i, column %i\n", i + 1, j + 1, i + y*l + 1, j + x*l + 1) : 0; } </code></pre> <p>Expected input on stdin looks like:</p> <pre><code>CODEGOLF 15 15 A I Y R J J Y T A S V Q T Z E X B X G R Z P W V T B K U F O ... </code></pre> <p>where <code>15 15</code> are the numbers of rows and columns, respectively.</p> <p>Since this is my first round of golf, and I could find precious little common tricks on the web, I'll list some that I found:</p> <ul> <li><p>Try to keep loop bodies to a single statement to save on moustaches (2 chars).<br> <em>Example</em>: anything using the comma operator. </p></li> <li><p>Use a conditional expression (<code>?:</code>) instead of an <code>if</code> when you can (1 char).<br> <em>Example</em>: instead of <code>if(x)printf("");</code>, write <code>x?printf(""):0;</code>. This works because <code>printf</code> returns <code>int</code>. </p></li> <li><p>Use the fact that booleans are <code>0</code> or <code>1</code> when used as an <code>int</code> (? chars).<br> <em>Example</em>: instead of <code>(y&gt;0?r-l:r)</code>, write <code>r-(y&gt;0)*l</code>. The savings here are in the parentheses, which were necessary in this situation.</p></li> <li><p><code>while</code> loops are never shorter than <code>for</code> loops, and longer most of the time (>= 0 chars).<br> <em>Example</em>: <code>while(x)y;</code> is just as long as <code>for(;x;y);</code> but with <code>for</code> you get the loop body to play with as well, without paying for the extra <code>;</code>. </p></li> <li><p>Put your loop increments into the last expression that uses the loop counter (1 char).<br> <em>Example</em>: instead of <code>for(;x&lt;b;++x)printf("%i",x);</code>, write <code>for(;x&lt;b;)printf("%i",x++);</code>. </p></li> <li><p>Leave off <code>main</code>'s return type (4 chars). </p></li> <li><p>Leave off <code>main</code>'s <code>return</code> statement (9 chars). </p></li> <li><p>Use <code>&amp;</code> and <code>|</code> instead of <code>&amp;&amp;</code> and <code>||</code> whenever possible (1 char).<br> <em>Example</em>: instead of <code>if(x||y)</code>, write <code>if(x|y)</code>. This only works if you don't depend on the short-circuit behaviour of <code>&amp;&amp;</code> and <code>||</code>.</p></li> <li><p>Inline the <code>strlen</code> function and remove <code>#include&lt;string.h&gt;</code> (11 chars).</p></li> </ul> <p>If you don't care about compiler warnings:</p> <ul> <li>Do not include any headers (many chars).</li> </ul>
 

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