Print Concatenation of Zig | C ++ 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 program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printZigZagConcat ( string str , int n ) { if ( n == 1 ) { cout << str ; return ; } int len = str . length ( ) ; string arr [ n ] ; int row = 0 ; bool down ; for ( int i = 0 ; i < len ; ++ i ) { arr [ row ] . push_back ( str [ i ] ) ; if ( row == n - 1 ) down = false ; else if ( row == 0 ) down = true ; ( down ) ? ( row ++ ) : ( row -- ) ; } for ( int i = 0 ; i < n ; ++ i ) cout << arr [ i ] ; } int main ( ) { string str = " GEEKSFORGEEKS " ; int n = 3 ; printZigZagConcat ( str , n ) ; return 0 ; }