Open in App
Not now

# Find Strings formed by replacing prefixes of given String with given characters

• Difficulty Level : Medium
• Last Updated : 21 Nov, 2022

Given a String ( S ) of length N and with that String, we need to print a triangle out of it. The triangle should start with the given string and keeps shrinking downwards by removing one character from the beginning of the string. The spaces on the left side of the triangle should be replaced with dot characters ( ‘.’ ).

Examples:

Input: S = “Geeks”
Output
Geeks
.eeks
..eks
…ks
….s

Input: S = “Orange”
Output:
Orange
.range
..ange
…nge
….ge
…..e

There are 2 ways to print this Triangle pattern:

• Using For Loop.
• Using While Loop.

Let’s start discussing each of these methods in detail.

Approach:

The approach is to use three loops:

• One is to control the number of rows.
• The second is to control the dots before the String.
• The third is to Print the remaining String.

By using the concepts of the nested loop wecan easily print the pattern.

Follow the steps to solve the problem:

• Take the Input of the String S.
• Use three loops one is the outer loop to change the line and two inner loops one to print the dots before the String and the other to print the remaining String.
• The Outer loop ( i ) runs from 0 to length -1 times.
• The First Inner loop runs from 0 to i times to print the dot character.
•  The Second Inner loop runs from i to length – 1 time to print the remaining String.
• After these two loops print the string that is formed.

Below is the program to print The triangle using for loop:

## C++

```// Program to Print a,
// Triangle Pattern using a string S.
#include <bits/stdc++.h>
using namespace std;
void fun(string S)
{
// str variable to make a new string.
string str = "";

// len variable to calculate the length of the string.
int len = S.length();

// Outer loop to control the rows.
for (int i = 0; i < len; i++) {
str = "";

// First Inner loop to print the dot character.
for (int j = 0; j < i; j++)
str += ".";

// Second Inner loop to print the remaining string,
for (int j = i; j < len; j++)
str += S[j];

// Printing the row and going to the next line.
cout << str << "\n";
}
}

// Drivers code
int main()
{
string S = "Geeks";

// Function call
fun(S);

return 0;
}```

## Java

```/*package whatever //do not write package name here */
// Program to Print a,
// Triangle Pattern using a string S.
import java.io.*;

class GFG {

static void fun(String S)
{

// str variable to make a new string.
String str = "";

// len variable to calculate the length of the string.
int len = S.length();

// Outer loop to control the rows.
for (int i = 0; i < len; i++) {
str = "";

// First Inner loop to print the dot character.
for (int j = 0; j < i; j++)
str += ".";

// Second Inner loop to print the remaining string,
for (int j = i; j < len; j++)
str += S.charAt(j);

// Printing the row and going to the next line.
System.out.println(str);
}
}
public static void main (String[] args) {
String S="Geeks";

// function call
fun(S);
}
}

// This code is contributed by nmkiniqw7b.```

## Python3

```# Python code
# Triangle Pattern using a string S.
def fun(S):

# str variable to make a new string.
str = ""

# len variable to calculate the length of the string.
l =len(S)

# Outer loop to control the rows.
for i in range(0,l):
str = ""

# First Inner loop to print the dot character.
for j in range(0,i):
str += "."

# Second Inner loop to print the remaining string,
for j in range(i,l):
str += S[j]

# Printing the row and going to the next line.
print(str)

# Drivers code
S = "Geeks"

# Function call
fun(S)

# This code is contributed by ksam24000```

## C#

```// C# implementation

using System;

public class GFG {

public static void fun(string S)
{
// str variable to make a new string.
string str = "";

// len variable to calculate the length of the
// string.
int len = S.Length;

// Outer loop to control the rows.
for (int i = 0; i < len; i++) {
str = "";

// First Inner loop to print the dot character.
for (int j = 0; j < i; j++)
str += ".";

// Second Inner loop to print the remaining
// string,
for (int j = i; j < len; j++)
str += S[j];

// Printing the row and going to the next line.
Console.WriteLine(str);
}
}

static public void Main()
{
string S = "Geeks";
// Function call
fun(S);
}
}
// this code is contributed by ksam24000```

## Javascript

```// JavaScript implementation
// Triangle Pattern using a string S.
function fun(S)
{
// str variable to make a new string.
let str = "";

// len variable to calculate the length of the string.
let len = S.length;

// Outer loop to control the rows.
for (let i = 0; i < len; i++) {
str = "";

// First Inner loop to print the dot character.
for (let j = 0; j < i; j++)
str += ".";

// Second Inner loop to print the remaining string,
for (let j = i; j < len; j++)
str += S[j];

// Printing the row and going to the next line.
console.log (str);
}
}

// Drivers code
S = "Geeks";

// Function call
fun(S);

// this code is contributed by ksam24000```
Output

```Geeks
.eeks
..eks
...ks
....s```

Time Complexity: O(N2), as the nested loop, is used.
Auxiliary Space: O(N), as we are creating a new string and reusing it.

Below is the program to print the triangle using the while loop:

## C++

```// Program to Print a,
// Triangle Pattern using a string S.
#include <bits/stdc++.h>
using namespace std;
void fun(string S)
{

// str variable to make a new string.
string str = "";

// len variable to calculate the
// length of the string.
int len = S.length();

// Outer loop to control the rows.
int i = 0;
while (i < len) {
str = "";

// First Inner loop to print
// the dot character.
int j = 0;
while (j < i) {
str += ".";
j++;
}

// Second Inner loop to print
// the remaining string,
int k = i;
while (k < len) {
str += S[k];
k++;
}
// Printing the row and going
// to the next line.
cout << str << "\n";

// Incrementing the loop
// control variable.
i++;
}
}

// Drivers code
int main()
{
string S = "Geeks";

// Function call
fun(S);

return 0;
}```

## Java

```/*package whatever //do not write package name here */
import java.io.*;
class GFG {

public static void fun(String S)
{

// str variable to make a new string.
String str = "";

// len variable to calculate the
// length of the string.
int len = S.length();

// Outer loop to control the rows.
int i = 0;
while (i < len) {
str = "";

// First Inner loop to print
// the dot character.
int j = 0;
while (j < i) {
str += ".";
j++;
}

// Second Inner loop to print
// the remaining string,
int k = i;
while (k < len) {
str += S.charAt(k);
k++;
}
// Printing the row and going
// to the next line.
System.out.println(str);

// Incrementing the loop
// control variable.
i++;
}
}

public static void main (String[] args)
{

// Code
String S = "Geeks";

// Function call
fun(S);
}
}

// This code is contributed by aadityaburujwale.```

## Python3

```# Program to Print a,
# Triangle Pattern using a string S.

def fun(S):
# str variable to make a new string.
str = ""

# len variable to calculate the
# length of the string.
length = len(S);

# Outer loop to control the rows.
i = 0
while (i < length):
str = ""

# First Inner loop to print
# the dot character.
j = 0
while (j < i):
str += "."
j=j+1

# Second Inner loop to print
# the remaining string,
k = i
while (k < length):
str += S[k]
k += 1

# Printing the row and going
# to the next line.
print(str)

# Incrementing the loop
# control variable.
i+=1

# Drivers code
S = "Geeks"

# Function call
fun(S)

# This code is contributed by akashish__
```

## C#

```using System;

public class GFG {

public static void fun(string S)
{

// str variable to make a new string.
string str = "";

// len variable to calculate the
// length of the string.
int len = S.Length;

// Outer loop to control the rows.
int i = 0;
while (i < len) {
str = "";

// First Inner loop to print
// the dot character.
int j = 0;
while (j < i) {
str += ".";
j++;
}

// Second Inner loop to print
// the remaining string,
int k = i;
while (k < len) {
str += S[k];
k++;
}
// Printing the row and going
// to the next line.
Console.WriteLine(str);

// Incrementing the loop
// control variable.
i++;
}
}

// Drivers code
static public void Main()
{

// Code
string S = "Geeks";

// Function call
fun(S);
}
}
// contributed by akashish__```

## Javascript

```<script>

// Program to Print a,
// Triangle Pattern using a string S.

function fun(S) {

// str variable to make a new string.
let str = "";

// len variable to calculate the
// length of the string.
let len = S.length;

// Outer loop to control the rows.
let i = 0;
while (i < len) {
str = "";

// First Inner loop to print
// the dot character.
let j = 0;
while (j < i) {
str += ".";
j++;
}

// Second Inner loop to print
// the remaining string,
let k = i;
while (k < len) {
str += S[k];
k++;
}
// Printing the row and going
// to the next line.
console.log(str);

// Incrementing the loop
// control variable.
i++;
}
}

// Drivers code
let S = "Geeks";

// Function call
fun(S);

// This code is contributed by akashish__

</script>```
Output

```Geeks
.eeks
..eks
...ks
....s```

Time Complexity: O(N2), as the nested loop, is used.
Auxiliary Space: O(1), as we are creating a new string and reusing it.

My Personal Notes arrow_drop_up
Related Articles