1

The question is to find all possible substrings in a given string.

I know this question is somewhat similar to this.

Generate all unique substrings for given string

But i'm trying it on my own. I have generated all possible combinations but problem is i want to eliminate some strings with space in them.

int main()
{
    int t;
    cin>>t;
    while(t>0){
        long long int n;
        cin>>n;
        string S;
        cin>>S;
        for(int i=1; i<=S.size(); i++) {
            for(int j=0; j<=S.size()-1; j++) {

                cout<<S.substr(j,i)<<endl;
            }
        }

        t--;
    }
    return 0;
}

input:

1
3
abb 

Actual output:

a
b
b
ab
bb
b
abb
bb
b

Expected output:

a
b
b
ab
bb
abb

As you can see there are some terms with space in them which i want to eliminate like 6th,8th and 9th terms in Actual output.

  • Hint: for a string of length 3, `S.substr(1,2)` and `S.substr(1,3)`, and in fact also `S.substr(1, 1000)` all produce the same substring. – Igor Tandetnik Apr 12 '19 at 15:19

1 Answers1

0

From the documentation of std::string::substr

The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).

You are under the misconception that the second argument of std::string::substr is the end position, but in fact it is an offset from the first argument.

user3217278
  • 320
  • 3
  • 9