Monday, September 26, 2022
HomeSoftware DevelopmentDiscover Strings shaped by changing prefixes of given String with given characters

# Discover Strings shaped by changing prefixes of given String with given characters

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
….s

Enter: 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.

Strategy:

The strategy 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.

Comply with the steps to resolve the issue:

• Take the Enter of the String S.
• Use three loops one is the outer loop to alter the road and two internal 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 Interior loop runs from 0 to i occasions to print the dot character.
•  The Second Interior loop runs from i to size – 1 time to print the remaining String.
• After these two loops print the string that’s shaped.

Under is this system to print The triangle utilizing for loop:

## C++

 `#embody ``utilizing` `namespace` `std;``void` `enjoyable(string S)``{``    ``    ``string str = ``""``;`` ` `    ``    ``int` `len = S.size();`` ` `    ``    ``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` `important()``{``    ``string S = ``"Geeks"``;`` ` `    ``    ``enjoyable(S);`` ` `    ``return` `0;``}`
Output

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

Time Complexity: O(N2), because the nested loop, is used.
Auxiliary House: 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++

 `#embody ``utilizing` `namespace` `std;``void` `enjoyable(string S)``{`` ` `    ``    ``string str = ``""``;`` ` `    ``    ``    ``int` `len = S.size();`` ` `    ``    ``int` `i = 0;``    ``whereas` `(i < len) {``        ``str = ``""``;`` ` `        ``        ``        ``int` `j = 0;``        ``whereas` `(j < i) {``            ``str += ``"."``;``            ``j++;``        ``}`` ` `        ``        ``        ``int` `ok = i;``        ``whereas` `(ok < len) {``            ``str += S[k];``            ``ok++;``        ``}``        ``        ``        ``cout << str << ``"n"``;`` ` `        ``        ``        ``i++;``    ``}``}`` ` `int` `important()``{``    ``string S = ``"Geeks"``;`` ` `    ``    ``enjoyable(S);`` ` `    ``return` `0;``}`
Output

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

Time Complexity: O(N2), because the nested loop, is used.
Auxiliary House: O(1), as we’re creating a brand new string and reusing it.

RELATED ARTICLES