text: Program to print Step Pattern | C ++ program to print Step Pattern ; function to print the steps ; declare a flag ; traverse through all the characters in the string ; if the x value is 0. . then we must increment till n ... set flag to true ; if the x value is n - 1 then we must decrement till 0 ... set flag as false ; print x * s ; checking whether to increment or decrement x ; Driver code ; Get the String and the number n ; calling the function
code: #include <iostream> NEW_LINE 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 ] << " STRNEWLINE " ; 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 ; }