Friday, September 9, 2022
HomeWordPress DevelopmentSample of Strings - GeeksforGeeks

Sample of Strings – GeeksforGeeks


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Given a string S of size N, discover the sample of the strings as proven under within the examples.

Examples: 

Enter: S = “Geek”
Output: Geek, Gee, Ge, G
Rationalization: Lower one character after every line

Enter: S = “G*g” 
Output: G*g, G*, G
Rationalization: Lower one character after every line

Utilizing two Nested loops:

Use the next thought to resolve the issue:

The concept to resolve this downside relies on the truth that now we have to print string N time in several traces and print the string by lowering the size of the string from the tip every time. 

So, iterate two nested loops one for printing string into totally different traces for N time and different loop for print the string by lowering the size of the string.

Comply with the under steps to resolve the issue:

  • Discover the size of the given string say N
  • Run a loop on i from 0 until i < N 
    • Run nested loop on j from 0 until j < N – i
      • Print character on the jth index of the string
    • Print the following line character i.e. ‘n’

Under is the implementation of the above strategy.

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

void printPattern(string& s)

{

    

    int n = s.measurement();

  

    

    for (int i = 0; i < n; i++) {

  

        

        for (int j = 0; j < n - i; j++) {

            cout << s[j];

        }

        cout << 'n';

    }

}

  

int fundamental()

{

    string s = "GeeK";

  

    

    printPattern(s);

    return 0;

}

Time Complexity: O(N2)
Auxiliary House: O(1)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments