Print a String in wave pattern


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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print wave pattern 
// of a given string
// This is a modified code of
  
#include<bits/stdc++.h>
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 strin 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<len; i++)
    {
        // put characters in the matrix
        a[row][i] = s[i]; 
          
        // You have reached the bottom
        if (row==n-1)
            down = false;     
        else if (row==0)
            down = true;    
              
        (down)?(row++):(row--);
    }
      
    // Print the Zig-Zag String
    for (int i=0; i<n; i++)
    
        for (int j=0; j<len; j++)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
}
  
// Driver function
int main()
{
    string s = "GeeksforGeeks";
    int n = 3;
    fun(s, n);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
        // strin 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php program to print wave 
// pattern of a given string
  
// Function that takes string
// and zigzag offset
function fun($s, $n
{
      
    // if offset is 1
    if ($n == 1)     
    {
          
        // simply print the 
        // strin and return
        echo $s;         
        return;
    }
  
    // Get length of the string
    $len = strlen($s); 
      
    // for counting the rows 
    // of the ZigZag
    $row = 0;     
    $down;
      
    for ($i = 0; $i < $len; $i++)
        for ($j = 0; $j < $len; $j++)
        $a[$i][$j]=" ";
      
    for ($i = 0; $i < $len; $i++)
    {
          
        // put characters
        // in the matrix
        $a[$row][$i] = $s[$i]; 
          
        // You have reached 
        // the bottom
        if ($row == $n - 1)
            $down = false; 
        else if ($row == 0)
            $down = true;
              
        ($down)? ($row++): ($row--);
    }
      
    // Print the Zig-Zag String
    for ($i = 0; $i < $n; $i++)
    
        for ($j = 0; $j < $len; $j++)
        {
            echo $a[$i][$j]." ";
        }
        echo "\n";
    }
}
  
    // Driver code
    $s = "GeeksforGeeks";
    $n = 3;
    fun($s, $n);
  
//This code is contributed by mits 
?>

chevron_right



Output:

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

This article is contributed by Nikhil Rawat. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Mithun Kumar, Ita_c