Print Concatenation of Zig | C ++ Program for above approach ; Function for zig - zag Concatenation ; Check is n is less or equal to 1 ; Iterate rowNum from 0 to n - 1 ; Iterate i till s . length ( ) - 1 ; Check is rowNum is 0 or n - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string zigZagConcat ( string s , int n ) { if ( n <= 1 ) { return s ; } string result = " " ; for ( int rowNum = 0 ; rowNum < n ; rowNum ++ ) { int i = rowNum ; bool up = true ; while ( i < s . length ( ) ) { result += s [ i ] ; if ( rowNum == 0 rowNum == n - 1 ) { i += ( 2 * n - 2 ) ; } else { if ( up ) { i += ( 2 * ( n - rowNum ) - 2 ) ; } else { i += rowNum * 2 ; } up ^= true ; } } } return result ; } int main ( ) { string str = " GEEKSFORGEEKS " ; int n = 3 ; cout << zigZagConcat ( str , n ) ; }