Print Concatenation of Zig | Java program to print string obtained by concatenation of different rows of Zig - Zag fashion ; 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
import java . util . Arrays ; class GFG { static void printZigZagConcat ( String str , int n ) { if ( n == 1 ) { System . out . print ( str ) ; return ; } char [ ] str1 = str . toCharArray ( ) ; int len = str . length ( ) ; String [ ] arr = new String [ n ] ; Arrays . fill ( arr , " " ) ; int row = 0 ; boolean down = true ; for ( int 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 ( int i = 0 ; i < n ; ++ i ) { System . out . print ( arr [ i ] ) ; } } public static void main ( String [ ] args ) { String str = " GEEKSFORGEEKS " ; int n = 3 ; printZigZagConcat ( str , n ) ; } }