Friday, October 7, 2022
HomeSoftware DevelopmentDiscover Strings fashioned by changing prefixes of given String with given characters

Discover Strings fashioned by changing prefixes of given String with given characters


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

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 aspect of the triangle needs 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.

Method:

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.

Through the use of the ideas of the nested loop wecan simply print the sample.

Observe the steps to resolve 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 instances.
    • The First Internal loop runs from 0 to i instances to print the dot character.
    •  The Second Internal 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++

#embrace <bits/stdc++.h>

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 most 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 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++

#embrace <bits/stdc++.h>

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 most 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 Area: O(1), as we’re creating a brand new string and reusing it.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments