# Print Concatenation of Zig-Zag String in ‘n’ Rows

Given a string and number of rows ‘n’. Print the string formed by concatenating n rows when input string is written in row-wise Zig-Zag fashion.

Examples:

```Input: str = "ABCDEFGH"
n = 2
Output: "ACEGBDFH"
Explanation: Let us write input string in Zig-Zag fashion
in 2 rows.
A   C   E   G
B   D   F   H
Now concatenate the two rows and ignore spaces
in every row. We get "ACEGBDFH"

Input: str = "GEEKSFORGEEKS"
n = 3
Output: GSGSEKFREKEOE
Explanation: Let us write input string in Zig-Zag fashion
in 3 rows.
G       S       G       S
E   K   F   R   E   K
E       O       E
Now concatenate the two rows and ignore spaces
in every row. We get "GSGSEKFREKEOE"```

## We strongly recommend that you click here and practice it, before moving on to the solution.

The idea is to traverse the input string. Every character has to go to one of the rows. One by one add all characters to different rows. Below is algorithm:

```1) Create an array of n strings, arr[n]
2) Initialize direction as "down" and row as 0. The
direction indicates whether we need to move up or
down in rows.
3) Traverse the input string, do following for every
character.
a) Append current character to string of current row.
b) If row number is n-1, then change direction to 'up'
c) If row number is 0, then change direction to 'down'
d) If direction is 'down', do row++.  Else do row--.
4) One by one print all strings of arr[]. ```

Below is implementation of above idea.

## C++

 `// C++ program to print string obtained by concatenation ` `// of different rows of Zig-Zag fashion ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Prints concatenation of all rows of str's Zig-Zag fasion ` `void` `printZigZagConcat(string str, ``int` `n) ` `{ ` `    ``// Corner Case (Only one row) ` `    ``if` `(n == 1) ` `    ``{ ` `        ``cout << str;       ` `        ``return``; ` `    ``}    ` ` `  `    ``// Find length of string ` `    ``int` `len = str.length(); ` ` `  `    ``// Create an array of strings for all n rows ` `    ``string arr[n]; ` ` `  `    ``// Initialize index for array of strings arr[] ` `    ``int` `row = 0; ` `    ``bool` `down; ``// True if we are moving down in rows,  ` `               ``// else false ` ` `  `    ``// Travers through given string ` `    ``for` `(``int` `i = 0; i < len; ++i) ` `    ``{ ` `        ``// append current character to current row ` `        ``arr[row].push_back(str[i]); ` ` `  `        ``// If last row is reached, change direction to 'up' ` `        ``if` `(row == n-1) ` `          ``down = ``false``; ` ` `  `        ``// If 1st row is reached, change direction to 'down' ` `        ``else` `if` `(row == 0) ` `          ``down = ``true``; ` ` `  `        ``// If direction is down, increment, else decrement ` `        ``(down)? (row++): (row--); ` `    ``} ` ` `  `    ``// Print concatenation of all rows ` `    ``for` `(``int` `i = 0; i < n; ++i) ` `        ``cout << arr[i]; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``string str = ``"GEEKSFORGEEKS"``; ` `    ``int` `n = 3; ` `    ``printZigZagConcat(str, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to print string  ` `// obtained by concatenation ` `// of different rows of  ` `// Zig-Zag fashion ` `import` `java.util.Arrays; ` ` `  `class` `GFG { ` ` `  `    ``// Prints concatenation  ` `    ``// of all rows of str's  ` `    ``// Zig-Zag fasion ` `    ``static` `void` `printZigZagConcat(String str, ` `            ``int` `n)  ` `    ``{ ` ` `  `        ``// Corner Case (Only one row) ` `        ``if` `(n == ``1``)  ` `        ``{ ` `            ``System.out.print(str); ` `            ``return``; ` `        ``} ` `        ``char``[] str1 = str.toCharArray(); ` ` `  `        ``// Find length of string ` `        ``int` `len = str.length(); ` ` `  `        ``// Create an array of ` `        ``// strings for all n rows ` `        ``String[] arr = ``new` `String[n]; ` `        ``Arrays.fill(arr, ``""``); ` ` `  `        ``// Initialize index for ` `        ``// array of strings arr[] ` `        ``int` `row = ``0``; ` `        ``boolean` `down = ``true``; ``// True if we are moving  ` `        ``// down in rows, else false ` ` `  `        ``// Travers through ` `        ``// given string ` `        ``for` `(``int` `i = ``0``; i < len; ++i)  ` `        ``{ ` `            ``// append current character ` `            ``// to current row ` `            ``arr[row] += (str1[i]); ` ` `  `            ``// If last row is reached, ` `            ``// change direction to 'up' ` `            ``if` `(row == n - ``1``)  ` `            ``{ ` `                ``down = ``false``; ` `            ``}  ` `             `  `            ``// If 1st row is reached,  ` `            ``// change direction to 'down' ` `            ``else` `if` `(row == ``0``)  ` `            ``{ ` `                ``down = ``true``; ` `            ``} ` ` `  `            ``// If direction is down,  ` `            ``// increment, else decrement ` `            ``if` `(down) ` `            ``{ ` `                ``row++; ` `            ``}  ` `            ``else`  `            ``{ ` `                ``row--; ` `            ``} ` `        ``} ` ` `  `        ``// Print concatenation ` `        ``// of all rows ` `        ``for` `(``int` `i = ``0``; i < n; ++i)  ` `        ``{ ` `            ``System.out.print(arr[i]); ` `        ``} ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String str = ``"GEEKSFORGEEKS"``; ` `        ``int` `n = ``3``; ` `        ``printZigZagConcat(str, n); ` `    ``} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## Python 3

 `# Python 3 program to print ` `# string obtained by  ` `# concatenation of different ` `# rows of Zig-Zag fashion ` ` `  `# Prints concatenation of all  ` `# rows of str's Zig-Zag fasion ` `def` `printZigZagConcat(``str``, n): ` `     `  `    ``# Corner Case (Only one row) ` `    ``if` `n ``=``=` `1``: ` `        ``print``(``str``)      ` `        ``return` ` `  `    ``# Find length of string ` `    ``l ``=` `len``(``str``) ` ` `  `    ``# Create an array of  ` `    ``# strings for all n rows ` `    ``arr``=``["" ``for` `x ``in` `range``(l)] ` ` `  `    ``# Initialize index for  ` `    ``# array of strings arr[] ` `    ``row ``=` `0` `     `  `    ``# Travers through ` `    ``# given string ` `    ``for` `i ``in` `range``(l): ` `         `  `        ``# append current character ` `        ``# to current row ` `        ``arr[row] ``+``=` `str``[i] ` ` `  `        ``# If last row is reached,  ` `        ``# change direction to 'up' ` `        ``if` `row ``=``=` `n ``-` `1``: ` `            ``down ``=` `False` ` `  `        ``# If 1st row is reached, ` `        ``# change direction to 'down' ` `        ``elif` `row ``=``=` `0``: ` `            ``down ``=` `True` ` `  `        ``# If direction is down,  ` `        ``# increment, else decrement ` `        ``if` `down: ` `            ``row ``+``=` `1` `        ``else``: ` `            ``row ``-``=` `1` ` `  `    ``# Print concatenation ` `    ``# of all rows ` `    ``for` `i ``in` `range``(n): ` `        ``print``(arr[i], end ``=` `"") ` ` `  `# Driver Code ` `str` `=` `"GEEKSFORGEEKS"` `n ``=` `3` `printZigZagConcat(``str``, n) ` ` `  `# This code is contributed ` `# by ChitraNayal `

## C#

 `// C# program to print string  ` `// obtained by concatenation ` `// of different rows of  ` `// Zig-Zag fashion ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Prints concatenation  ` `    ``// of all rows of str's  ` `    ``// Zig-Zag fasion ` `    ``static` `void` `printZigZagConcat(``string` `str, ` `                                  ``int` `n) ` `    ``{ ` `         `  `    ``// Corner Case (Only one row) ` `    ``if` `(n == 1) ` `    ``{ ` `        ``Console.Write(str);      ` `        ``return``; ` `    ``}  ` `     `  `    ``char``[] str1 = str.ToCharArray(); ` `     `  `    ``// Find length of string ` `    ``int` `len = str.Length; ` ` `  `    ``// Create an array of ` `    ``// strings for all n rows ` `    ``string` `[]arr = ``new` `string``[n]; ` ` `  `    ``// Initialize index for ` `    ``// array of strings arr[] ` `    ``int` `row = 0; ` `    ``bool` `down = ``true``; ``// True if we are moving  ` `                      ``// down in rows, else false ` ` `  `    ``// Travers through ` `    ``// given string ` `    ``for` `(``int` `i = 0; i < len; ++i) ` `    ``{ ` `        ``// append current character ` `        ``// to current row ` `        ``arr[row] += (str1[i]); ` ` `  `        ``// If last row is reached, ` `        ``// change direction to 'up' ` `        ``if` `(row == n - 1) ` `        ``down = ``false``; ` ` `  `        ``// If 1st row is reached,  ` `        ``// change direction to 'down' ` `        ``else` `if` `(row == 0) ` `        ``down = ``true``; ` ` `  `        ``// If direction is down,  ` `        ``// increment, else decrement ` `        ``if``(down) ` `        ``row++; ` `        ``else` `        ``row--; ` `    ``} ` ` `  `    ``// Print concatenation ` `    ``// of all rows ` `    ``for` `(``int` `i = 0; i < n; ++i) ` `        ``Console.Write(arr[i]); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``String str = ``"GEEKSFORGEEKS"``; ` `        ``int` `n = 3; ` `        ``printZigZagConcat(str, n); ` `    ``} ` `} ` ` `  `// This code is contributed  ` `// by ChitraNayal `

Output:

`GSGSEKFREKEOE`

Time Complexity: O(len) where len is length of input string.
Auxiliary Space: O(len)

Thanks to Gaurav Ahirwar for suggesting above solution.

My Personal Notes arrow_drop_up

Improved By : chitranayal, princiraj1992

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.