# Print a String in wave pattern

• Difficulty Level : Easy
• Last Updated : 16 Sep, 2021

Given a string, you need to transform this string in the form of a wave pattern

Examples:

```Input : GeeksforGeeks, 2
Output :
G  e  s  o  G  e  s
e  k  f  r  e  k

Input : GeeksforGeeks, 4
Output :
G      o      s
e    f  r    k
e  s    G  e
k      e    ```

## C++

 `// CPP program to print wave pattern``// of a given string``// This is a modified code of``// https://www.geeksforgeeks.org/print-concatenation-of-zig-zag-string-form-in-n-rows/` `#include``using` `namespace` `std;` `// Function that takes string and zigzag offset``void` `fun(string s, ``int` `n)``{``    ``// if offset is 1``    ``if` `(n==1)            ``    ``{``        ``// simply print the string and return``        ``cout << s;            ``        ``return``;``    ``}` `    ``// Get length of the string``    ``int` `len = s.length();``    ` `    ``// Create a 2d character array``    ``char` `a[len][len] = { };``    ` `    ``// for counting the rows of the ZigZag``    ``int` `row = 0;        ``    ``bool` `down;``    ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to print wave ``// pattern of a given string``import` `java.lang.*;``import` `java.util.*;``class` `GFG``{` `    ``// Function that takes``    ``// string and zigzag offset``    ``static` `void` `fun(String s, ``int` `n)``    ``{``        ` `    ``// if offset is 1``    ``if` `(n == ``1``)            ``    ``{``        ``// simply print the``        ``// string and return``        ``System.out.print(s);            ``        ``return``;``    ``}` `    ``// Get length of the string``    ``int` `len = s.length();``    ` `    ``// Create a 2d character array``    ``char` `[][]a = ``new` `char``[len][len];``    ``char` `[]c = s.toCharArray();``    ` `    ``// for counting the``    ``// rows of the ZigZag``    ``int` `row = ``0``;        ``    ``boolean` `down = ``true``;``    ` `    ``for` `(``int` `i = ``0``; i < len; i++)``    ``{``        ``// put characters in``        ``// the matrix``        ``a[row][i] = c[i];``        ` `        ``// You have reached``        ``// the bottom``        ``if` `(row == n - ``1``)``            ``down = ``false``;    ``        ``else` `if` `(row == ``0``)``            ``down = ``true``;``            ` `        ``if``(down)``            ``row++;``        ``else``            ``row--;``    ``}``    ` `    ``// Print the Zig-Zag String``    ``for` `(``int` `i = ``0``; i < n; i++)``    ``{``        ``for` `(``int` `j = ``0``; j < len; j++)``        ``{``            ``System.out.print(a[i][j] + ``" "``);``        ``}``        ``System.out.println();``    ``}``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``String s = ``"GeeksforGeeks"``;``        ``int` `n = ``3``;``        ``fun(s, n);``    ``}``}` `// This code is contributed``// by ChitraNayal`

## Python 3

 `# Function that takes string``# and zigzag offset``def` `fun(s, n):``    ` `    ``# if offset is 1``    ``if` `(n ``=``=` `1``):``        ` `        ``# simply print the``        ``# string and return``        ``print``(s)            ``        ``return` `    ``# Get length of the string``    ``l ``=` `len``(s)``    ` `    ``# Create a 2d character array``    ``a ``=` `[[``" "` `for` `x ``in` `range``(l)] ``for` `y ``in` `range``(l)]` `    ``# for counting the``    ``# rows of the ZigZag``    ``row ``=` `0``    ``for` `i ``in` `range``(l):``        ` `        ``# put characters in the matrix``        ``a[row][i] ``=` `s[i];``    ` `        ``# You have reached the bottom``        ``if` `row ``=``=` `n ``-` `1``:``            ``down ``=` `False`   `        ``elif` `row ``=``=` `0``:``            ``down ``=` `True``        ``if` `down ``=``=` `True``:``            ``row ``=` `row ``+` `1``        ``else``:``            ``row ``=` `row ``-` `1` `    ``# Print the Zig-Zag String``    ``for` `i ``in` `range``(n):``        ``for` `j ``in` `range``(l):``            ``print``(``str``(a[i][j]), end ``=` `" "``)``        ``print``()``    ` `# Driver Code``s ``=` `"GeeksforGeeks"``n ``=` `3``fun(s, n)` `# This code is contributed``# by ChitraNayal`

## C#

 `// C# program to print wave``// pattern of a given string``using` `System;` `class` `GFG``{` `    ``// Function that takes``    ``// string and zigzag offset``    ``static` `void` `fun(``string` `s, ``int` `n)``    ``{``        ` `    ``// if offset is 1``    ``if` `(n == 1)            ``    ``{``        ``// simply print the``        ``// string and return``        ``Console.Write(s);            ``        ``return``;``    ``}` `    ``// Get length of the string``    ``int` `len = s.Length;``    ` `    ``// Create a 2d character array``    ``char``[,] a = ``new` `char``[len,len];``    ``char``[] c = s.ToCharArray();``    ` `    ``// for counting the``    ``// rows of the ZigZag``    ``int` `row = 0;        ``    ``bool` `down = ``true``;``    ` `    ``for` `(``int` `i = 0; i < len; i++)``    ``{``        ``// put characters``        ``// in the matrix``        ``a[row, i] = c[i];``        ` `        ``// You have reached the bottom``        ``if` `(row == n - 1)``            ``down = ``false``;    ``        ``else` `if` `(row == 0)``            ``down = ``true``;``            ` `        ``if``(down)``            ``row++;``        ``else``            ``row--;``    ``}``    ` `    ``// Print the Zig-Zag String``    ``for` `(``int` `i = 0; i < n; i++)``    ``{``        ``for` `(``int` `j = 0; j < len; j++)``        ``{``            ``Console.Write(a[i, j] + ``" "``);``        ``}``        ``Console.Write(``"\n"``);``    ``}``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``string` `s = ``"GeeksforGeeks"``;``        ``int` `n = 3;``        ``fun(s, n);``    ``}``}` `// This code is contributed``// by ChitraNayal`

## PHP

 ``

## Javascript

 ``

Output:

```G       s       G       s
e   k   f   r   e   k
e       o       e     ```

My Personal Notes arrow_drop_up