Program to print Step Pattern
Last Updated :
14 Sep, 2022
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
- Set a flag to denote whether to increment or decrement
- Set a variable x to denote the number of *s to print and initialize it to 0
- Traverse through all the characters in the string
- for each character print x *s
- If the flag is set then increment
- else decrement
- If the x value is equal to n-1 then set the flag to false
- If the x value is equal to 0 then set the flag to true
Implementation:
C++
#include <iostream>
using namespace std;
void steps(string str, int n)
{
bool flag;
int x = 0;
for ( int i = 0; i < str.length(); i++) {
if (x == 0)
flag = true ;
if (x == n - 1)
flag = false ;
for ( int j = 0; j < x; j++)
cout << "*" ;
cout << str[i] << "\n" ;
if (flag == true )
x++;
else
x--;
}
}
int main()
{
int n = 4;
string str = "GeeksForGeeks" ;
cout << "String: " << str << endl;
cout << "Max Length of Steps: "
<< n << endl;
steps(str, n);
return 0;
}
|
Java
import java.util.*;
class solution
{
static void steps(String str, int n)
{
boolean flag = false ;
int x = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
if (x == 0 )
flag = true ;
if (x == n - 1 )
flag = false ;
for ( int j = 0 ; j < x; j++)
System.out.print( "*" );
System.out.print(str.charAt(i)+ "\n" );
if (flag == true )
x++;
else
x--;
}
}
public static void main(String args[])
{
int n = 4 ;
String str = "GeeksForGeeks" ;
System.out.println( "String: " +str);
System.out.println( "Max Length of Steps: " +n);
steps(str, n);
}
}
|
Python3
import math as mt
def steps(string, n):
flag = False
x = 0
for i in range ( len (string)):
if (x = = 0 ):
flag = True
if (x = = n - 1 ):
flag = False
for j in range (x):
print ( "*" , end = "")
print (string[i])
if (flag = = True ):
x + = 1
else :
x - = 1
n = 4
string = "GeeksForGeeks"
print ( "String: " , string)
print ( "Max Length of Steps: " , n)
steps(string, n)
|
C#
using System;
public class solution
{
public static void steps( string str, int n)
{
bool flag = false ;
int x = 0;
for ( int i = 0; i < str.Length; i++)
{
if (x == 0)
{
flag = true ;
}
if (x == n - 1)
{
flag = false ;
}
for ( int j = 0; j < x; j++)
{
Console.Write( "*" );
}
Console.Write(str[i] + "\n" );
if (flag == true )
{
x++;
}
else
{
x--;
}
}
}
public static void Main( string [] args)
{
int n = 4;
string str = "GeeksForGeeks" ;
Console.WriteLine( "String: " + str);
Console.WriteLine( "Max Length of Steps: " + n);
steps(str, n);
}
}
|
PHP
<?php
function steps( $str , $n )
{
$x = 0;
for ( $i = 0; $i < strlen ( $str ); $i ++)
{
if ( $x == 0)
$flag = true;
if ( $x == $n - 1)
$flag = false;
for ( $j = 0; $j < $x ; $j ++)
echo "*" ;
echo $str [ $i ], "\n" ;
if ( $flag == true)
$x ++;
else
$x --;
}
}
$n = 4;
$str = "GeeksForGeeks" ;
echo "String: " , $str , "\n" ;
echo "Max Length of Steps: " , $n , "\n" ;
steps( $str , $n );
?>
|
Javascript
<script>
function steps(str, n) {
var flag;
var x = 0;
for ( var i = 0; i < str.length; i++) {
if (x == 0) flag = true ;
if (x == n - 1) flag = false ;
for ( var j = 0; j < x; j++) document.write( "*" );
document.write(str[i] + "<br>" );
if (flag == true ) x++;
else x--;
}
}
var n = 4;
var str = "GeeksForGeeks" ;
document.write( "String: " + str + "<br>" );
document.write( "Max Length of Steps: " + n + "<br>" );
steps(str, n);
</script>
|
Output
String: GeeksForGeeks
Max Length of Steps: 4
G
*e
**e
***k
**s
*F
o
*r
**G
***e
**e
*k
s
Complexity Analysis:
- Time complexity: O(m*n) where m is the length of the string
- Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...