fork download
  1. //i;j;d;f(char*s,int n){for(i=0;i<n;i++)for(j=0;j+i<strlen(s);j=d+i+(n<2)){d=j-i+2*n-2;putchar(s[i+j]);if(i>0&i<n-1&d<strlen(s))putchar(s[d]);}}
  2. //t;i;j;d;f(s,n)char*s;{for(t=strlen(s),i=0;i<n;i++)for(j=0;j+i<t;j=d+i+(n<2))d=j-i+2*~-n,putchar(s[i+j]),i>0&i<n-1&d<t&&putchar(s[d]);}
  3.  
  4. // C variable and function declaration magic
  5. t;i;j;d;f(s,n)char*s;{
  6. // Iterate through each "row" of the string
  7. for(t=strlen(s),i=0;++i<n;)
  8. // Iterate through each element on the row
  9. // The original index iterator here was j+=2*(n-1), which is a full "zig-zag" forward
  10. // The (n<2) is for the edge case of n==1, which will break the existing logic.
  11. for(j=0; j+i<t; j=d+i+(n<2))
  12. // If j+i is the "zig", d is the "zag": Original index was d=j+i+2*(n-i-1)
  13. // Two's complement swag here courtesy of Jonathan Frech
  14. d=j-i+2*~-n,
  15. putchar(s[i+j]),
  16. // Short circuit logic to write the "zag" character if we're not on the first or last row
  17. i>0 & i<n-1 & d<t && putchar(s[d]);
  18. }
  19.  
  20. main(){
  21. f("WATERMELON",3); putchar('\n');
  22. f("WATERMELON",4); putchar('\n');
  23. f("programmingpuzzles",5); putchar('\n');
  24. f("codegolf",3); putchar('\n');
  25. f("elephant",4); putchar('\n');
  26. f("1234567",3); putchar('\n');
  27. f("ABABABABAB",2); putchar('\n');
  28. f("qwertyuiop",1); putchar('\n');
  29.  
  30. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
AEMLNTE
AMLTROEN
rmnlsomgzgapzru
oeofdl
latehp
24637
BBBBB