El programa debe aceptar una string S y un número entero N como entrada. El programa debe imprimir el patrón deseado como se muestra a continuación:
Ejemplos:
Entrada: string = “abcdefghijk”, n = 3
Salida:
a
*b
**c
*d
e
*f
**g
*h
i
*j
**k
Explicación:
Aquí N es 3. La altura más alta posible del patrón de string debe ser 3. Comience desde la altura como 1. Incremente hasta alcanzar el valor n(=3). Una vez alcanzada la altura, comience a disminuir. Repita el proceso hasta que se impriman todos los caracteres de la string.
Entrada: string = «GeeksForGeeks», n = 4
Salida:
g
*e
**e
***k
**s
*f
o
*r
**g
***e
**e
*k
s
Acercarse
- Establezca una bandera para indicar si se debe incrementar o disminuir
- Establezca una variable x para indicar el número de *s para imprimir e inicialícelo en 0
- Atraviesa todos los caracteres de la string.
- para cada caracter imprime x *s
- Si la bandera está configurada, entonces incremente
- de lo contrario decremento
- Si el valor de x es igual a n-1, establezca el indicador en falso
- Si el valor de x es igual a 0, establezca el indicador en verdadero
Implementación:
C++
// 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; }
Java
// 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
Python3
# 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
C#
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
PHP
<?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 ?>
Javascript
<script> // JavaScript program to print Step Pattern // function to print the steps function steps(str, n) { // declare a flag var flag; var x = 0; // traverse through all the characters in the string for (var 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 (var j = 0; j < x; j++) document.write("*"); document.write(str[i] + "<br>"); // checking whether to // increment or decrement x if (flag == true) x++; else x--; } } // Get the String and the number n var n = 4; var str = "GeeksForGeeks"; document.write("String: " + str + "<br>"); document.write("Max Length of Steps: " + n + "<br>"); // calling the function steps(str, n); </script>
String: GeeksForGeeks Max Length of Steps: 4 G *e **e ***k **s *F o *r **G ***e **e *k s
Complejidad temporal: O(m*n) donde m es la longitud de la string
Espacio auxiliar: O(1)