Program to print Step Pattern

The program must accept a string S and an integer N as the input. The program must print the desired pattern as shown below:

Examples:

Input: string = “abcdefghijk”, n = 3
Output:
a
*b
**c
*d
e
*f
**g
*h
i
*j
**k
Explanation:
Here N is 3. The highest possible height of the string pattern must be 3. Start from the height as 1. Increment till the value n(=3) is reached. Once the height is reached start decrementing. Repeat the process until all the characters in the string are printed.



Input: string = “GeeksForGeeks”, n = 4
Output:
g
*e
**e
***k
**s
*f
o
*r
**g
***e
**e
*k
s

Approach

  1. Set a flag to denote whether to increment or decrement
  2. Set a variable x to denote the number of *s to print and initialize it to 0
  3. Traverse through all the characters in the string
  4. for each character print x *s
  5. If the flag is set then increment
  6. else decrement
  7. If the x value is equal to n-1 then set the flag to false
  8. If the x value is equal to 0 then set the flag to true

Implementation:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print Step Pattern
#include <iostream>
  
using namespace std;
  
// function to print the steps
void steps(string str, int n)
{
    // declare a flag
    bool flag;
    int x = 0;
  
    // traverse through all the characters in the string
    for (int i = 0; i < str.length(); i++) {
  
        // if the x value is 0.. then
        // we must increment till n ...
        // set flag to true
        if (x == 0)
            flag = true;
  
        // if the x value is n-1 then
        // we must decrement till 0 ...
        // set flag as false
        if (x == n - 1)
            flag = false;
  
        // print x *s
        for (int j = 0; j < x; j++)
            cout << "*";
  
        cout << str[i] << "\n";
  
        // checking whether to
        // increment or decrement x
        if (flag == true)
            x++;
        else
            x--;
    }
}
  
int main()
{
  
    // Get the String and the number n
    int n = 4;
    string str = "GeeksForGeeks";
  
    cout << "String: " << str << endl;
    cout << "Max Length of Steps: "
         << n << endl;
  
    // calling the function
    steps(str, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to print Step Pattern
import java.util.*;
  
class solution
{
  
// function to print the steps
static void steps(String str, int n)
{
    // declare a flag
    boolean flag = false;
    int x = 0;
  
    // traverse through all the characters in the string
    for (int i = 0; i < str.length(); i++) {
  
        // if the x value is 0.. then
        // we must increment till n ...
        // set flag to true
        if (x == 0)
            flag = true;
  
        // if the x value is n-1 then
        // we must decrement till 0 ...
        // set flag as false
        if (x == n - 1)
            flag = false;
  
        // print x *s
        for (int j = 0; j < x; j++)
            System.out.print("*");
  
        System.out.print(str.charAt(i)+"\n");
  
        // checking whether to
        // increment or decrement x
        if (flag == true)
            x++;
        else
            x--;
    }
}
  
public static void main(String args[])
{
  
    // Get the String and the number n
    int n = 4;
    String str = "GeeksForGeeks";
  
    System.out.println("String: "+str);
    System.out.println("Max Length of Steps: "+n);
  
    // calling the function
    steps(str, n);
  
}
  
// This code is contributed by
// Shashank_Sharma

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print Step Pattern
import math as mt 
  
# function to print the steps
def steps(string, n):
      
    # declare a flag
    flag = False
    x = 0
  
    # traverse through all the characters
    # in the string
    for i in range(len(string)):
  
        # if the x value is 0.. then
        # we must increment till n ...
        # set flag to true
        if (x == 0):
            flag = True
  
        # if the x value is n-1 then
        # we must decrement till 0 ...
        # set flag as false
        if (x == n - 1):
            flag = False
  
        # print x *s
        for j in range(x):
            print("*", end = "")
  
        print(string[i])
  
        # checking whether to
        # increment or decrement x
        if (flag == True):
            x += 1
        else:
            x -= 1
  
# Driver code
  
# Get the String and the number n
n = 4
string = "GeeksForGeeks"
  
print("String: ", string)
print("Max Length of Steps: ", n)
  
# calling the function
steps(string, n)
  
# This code is contributed
# by Mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

using System;
  
// C# Program to print Step Pattern 
  
public class solution
{
  
// function to print the steps 
public static void steps(string str, int n)
{
    // declare a flag 
    bool flag = false;
    int x = 0;
  
    // traverse through all the characters in the string 
    for (int i = 0; i < str.Length; i++)
    {
  
        // if the x value is 0.. then 
        // we must increment till n ... 
        // set flag to true 
        if (x == 0)
        {
            flag = true;
        }
  
        // if the x value is n-1 then 
        // we must decrement till 0 ... 
        // set flag as false 
        if (x == n - 1)
        {
            flag = false;
        }
  
        // print x *s 
        for (int j = 0; j < x; j++)
        {
            Console.Write("*");
        }
  
        Console.Write(str[i] + "\n");
  
        // checking whether to 
        // increment or decrement x 
        if (flag == true)
        {
            x++;
        }
        else
        {
            x--;
        }
    }
}
  
// Driver code
public static void Main(string[] args)
{
  
    // Get the String and the number n 
    int n = 4;
    string str = "GeeksForGeeks";
  
    Console.WriteLine("String: " + str);
    Console.WriteLine("Max Length of Steps: " + n);
  
    // calling the function 
    steps(str, n);
  
}
}
  
  // This code is contributed by shrikant13

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print Step Pattern 
  
// function to print the steps 
function steps($str, $n
    $x = 0; 
  
    // traverse through all the characters
    // in the string 
    for ($i = 0; $i < strlen($str); $i++)
    
  
        // if the x value is 0.. then 
        // we must increment till n ... 
        // set flag to true 
        if ($x == 0) 
            $flag = true; 
  
        // if the x value is n-1 then 
        // we must decrement till 0 ... 
        // set flag as false 
        if ($x == $n - 1) 
            $flag = false; 
  
        // print x *s 
        for ($j = 0; $j < $x; $j++) 
            echo "*"
  
        echo $str[$i], "\n"
  
        // checking whether to 
        // increment or decrement x 
        if ($flag == true) 
            $x++; 
        else
            $x--; 
    
  
// Driver Code
  
// Get the String and the number n 
$n = 4; 
$str = "GeeksForGeeks"
  
echo "String: ", $str, "\n"
echo "Max Length of Steps: ", $n, "\n"
  
// calling the function 
steps($str, $n); 
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

String: GeeksForGeeks
Max Length of Steps: 4
G
*e
**e
***k
**s
*F
o
*r
**G
***e
**e
*k
s


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.