title: What is a better solution for a changed zigzag printing?
body: <p>Consider the following transformation on a string.  Given as an input a number of rows and a string, write that string in a zigzag back and forth across the rows.  For example, given input</p>
  
  <blockquote>
    <p>Please convert me to a customized zigzag printing format</p>
  </blockquote>
  
  <p>with three rows, we would write the string in a zigzag like this:</p>
  
  <pre><code>P   s   n   t   o   s   i   z   a   i   n   r
   l a e o v r m t a u t m z d i z g r n i g o m t
   e   c   e   e   c   o   e   g   p   t   f   a
  </code></pre>
  
  <p>(please dont edit this print out, this is required output, it is not a real zigzag, it is
  a customized zigzag)</p>
  
  <p>Finally, we concatenate the contests of each row together to get the resulting string</p>
  
  <pre><code>Psntosizainrlaeovrmtautmzdizgrnigomteceecoegptfa
  </code></pre>
  
  <p>I have a sketch of an algorithm for this problem that I've written out in pseudocode here:</p>
  
  <pre><code>it is asking about the relationship between {index} and {output row and  col}
  Def:
    down = false;
    up = false; 
    r=0;
    c=0;
    char[][] output; 
  there are three cases here:
  case 1: 
    index % 4 == 0, it is the beginning of a down printing
    r = 0;
    output[r][c] = in.charAt(index);
    c++;
    down = true; up = false;
  
  case 2:
    index%4 != 0 &amp;&amp; down = true;
    r = index % 4;
    output[r][c] = in.charAt(index);
    if( r == 2){ up = true; down = false; c++;}  // when come to the last row of a down formatting
  
  case 3:
    index%4 != 0 &amp;&amp; up == true;
    r = 4- index%4;
    output[r][c] = in.charAt(index);
    c++;
  </code></pre>
  
  <p>For a general solution, i can replace 4 as <code>nRows+1</code>;</p>
  
  <p>While I think this works, I'm not too happy with it.  Does anyone have a clearer or faster algorithm for solving this problem?</p>