Quick Telecast
Expect News First

Find Strings formed by replacing prefixes of given String with given characters

0 43


View Discussion

Improve Article

Save Article

Like Article

View Discussion

Improve Article

Save Article

Like Article

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;

}

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;

}

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.


View Discussion

Improve Article

Save Article

Like Article

View Discussion

Improve Article

Save Article

Like Article

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;

}

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;

}

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.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Quick Telecast is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.