Given a String ( S ) of size N and with that String, we have to print a triangle out of it. The triangle ought to begin with the given string and retains shrinking downwards by eradicating one character from the start of the string. The areas on the left facet of the triangle ought to be changed with dot characters ( ‘.’ ).
Examples:
Enter: S = “Geeks”
Output:
Geeks
.eeks
..eks
…ks
….sEnter: S = “Orange”
Output:
Orange
.vary
..ange
…nge
….ge
…..e
There are 2 methods to print this Triangle sample:
- Utilizing For Loop.
- Utilizing Whereas Loop.
Let’s begin discussing every of those strategies intimately.
Method:
The method is to make use of three loops:
- One is to manage the variety of rows.
- The second is to manage the dots earlier than the String.
- The third is to Print the remaining String.
By utilizing the ideas of the nested loop wecan simply print the sample.
Observe the steps to unravel the issue:
- Take the Enter of the String S.
- Use three loops one is the outer loop to vary the road and two inside loops one to print the dots earlier than the String and the opposite to print the remaining String.
- The Outer loop ( i ) runs from 0 to size -1 occasions.
- The First Inside loop runs from 0 to i occasions to print the dot character.
- The Second Inside loop runs from i to size – 1 time to print the remaining String.
- After these two loops print the string that’s fashioned.
Under is this system to print The triangle utilizing for loop:
C++
|
Geeks .eeks ..eks ...ks ....s
Time Complexity: O(N2), because the nested loop, is used.
Auxiliary Area: O(N), as we’re creating a brand new string and reusing it.
Under is this system to print the triangle utilizing the whereas loop:
C++
|
Geeks .eeks ..eks ...ks ....s
Time Complexity: O(N2), because the nested loop, is used.
Auxiliary Area: O(1), as we’re creating a brand new string and reusing it.