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
….sInput: 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++
|
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++
|
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.
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
….sInput: 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++
|
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++
|
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.