Print Concatenation of Zig | Prints concatenation of all rows of str 's  Zig-Zag fashion ; Corner Case ( Only one row ) ; Find length of string ; Create an array of strings for all n rows ; Initialize index for array of strings arr [ ] ; True if we are moving down in rows , else false ; Traverse through given string ; append current character to current row ; If last row is reached , change direction to ' up ' ; If 1 st row is reached , change direction to ' down ' ; If direction is down , increment , else decrement ; Print concatenation of all rows ; Driver Code
function printZigZagConcat ( str , n ) { if ( n == 1 ) { document . write ( str ) ; return ; } let str1 = str . split ( " " ) ; let len = str . length ; let arr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = " " ; } let row = 0 ; let down = true ; for ( let i = 0 ; i < len ; ++ i ) { arr [ row ] += ( str1 [ i ] ) ; if ( row == n - 1 ) { down = false ; } else if ( row == 0 ) { down = true ; } if ( down ) { row ++ ; } else { row -- ; } } for ( let i = 0 ; i < n ; ++ i ) { document . write ( arr [ i ] ) ; } } let str = " " ; let n = 3 ; printZigZagConcat ( str , n ) ;