Find Strings formed by replacing prefixes of given String with given characters
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++
#include <bits/stdc++.h>
using namespace std;
void fun(string S)
{
string str = "" ;
int len = S.length();
for ( int i = 0; i < len; i++) {
str = "" ;
for ( int j = 0; j < i; j++)
str += "." ;
for ( int j = i; j < len; j++)
str += S[j];
cout << str << "\n" ;
}
}
int main()
{
string S = "Geeks" ;
fun(S);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void fun(String S)
{
String str = "" ;
int len = S.length();
for ( int i = 0 ; i < len; i++) {
str = "" ;
for ( int j = 0 ; j < i; j++)
str += "." ;
for ( int j = i; j < len; j++)
str += S.charAt(j);
System.out.println(str);
}
}
public static void main (String[] args) {
String S= "Geeks" ;
fun(S);
}
}
|
Python3
def fun(S):
str = ""
l = len (S)
for i in range ( 0 ,l):
str = ""
for j in range ( 0 ,i):
str + = "."
for j in range (i,l):
str + = S[j]
print ( str )
S = "Geeks"
fun(S)
|
C#
using System;
public class GFG {
public static void fun( string S)
{
string str = "" ;
int len = S.Length;
for ( int i = 0; i < len; i++) {
str = "" ;
for ( int j = 0; j < i; j++)
str += "." ;
for ( int j = i; j < len; j++)
str += S[j];
Console.WriteLine(str);
}
}
static public void Main()
{
string S = "Geeks" ;
fun(S);
}
}
|
Javascript
function fun(S)
{
let str = "" ;
let len = S.length;
for (let i = 0; i < len; i++) {
str = "" ;
for (let j = 0; j < i; j++)
str += "." ;
for (let j = i; j < len; j++)
str += S[j];
console.log (str);
}
}
S = "Geeks" ;
fun(S);
|
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++
#include <bits/stdc++.h>
using namespace std;
void fun(string S)
{
string str = "" ;
int len = S.length();
int i = 0;
while (i < len) {
str = "" ;
int j = 0;
while (j < i) {
str += "." ;
j++;
}
int k = i;
while (k < len) {
str += S[k];
k++;
}
cout << str << "\n" ;
i++;
}
}
int main()
{
string S = "Geeks" ;
fun(S);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void fun(String S)
{
String str = "" ;
int len = S.length();
int i = 0 ;
while (i < len) {
str = "" ;
int j = 0 ;
while (j < i) {
str += "." ;
j++;
}
int k = i;
while (k < len) {
str += S.charAt(k);
k++;
}
System.out.println(str);
i++;
}
}
public static void main (String[] args)
{
String S = "Geeks" ;
fun(S);
}
}
|
Python3
def fun(S):
str = ""
length = len (S);
i = 0
while (i < length):
str = ""
j = 0
while (j < i):
str + = "."
j = j + 1
k = i
while (k < length):
str + = S[k]
k + = 1
print ( str )
i + = 1
S = "Geeks"
fun(S)
|
C#
using System;
public class GFG {
public static void fun( string S)
{
string str = "" ;
int len = S.Length;
int i = 0;
while (i < len) {
str = "" ;
int j = 0;
while (j < i) {
str += "." ;
j++;
}
int k = i;
while (k < len) {
str += S[k];
k++;
}
Console.WriteLine(str);
i++;
}
}
static public void Main()
{
string S = "Geeks" ;
fun(S);
}
}
|
Javascript
<script>
function fun(S) {
let str = "" ;
let len = S.length;
let i = 0;
while (i < len) {
str = "" ;
let j = 0;
while (j < i) {
str += "." ;
j++;
}
let k = i;
while (k < len) {
str += S[k];
k++;
}
console.log(str);
i++;
}
}
let S = "Geeks" ;
fun(S);
</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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...