-1

I have a string "abc". Want to get all the different string from string character.

#include<bits/stdc++.h>
using namespace std;

// Function to print all sub strings
void subString(string s, int n) 
{
     // Pick starting point in outer loop
    // and lengths of different strings for
    // a given starting point
    for (int i = 0; i < n; i++) 
       for (int len = 1; len <= n - i; len++)
           cout << s.substr(i, len) << endl;
}

 // Driver program to test above function
 int main() 
 {
    string s = "abc";
    subString(s,s.length());
    return 0;
 }

Expecting :

a b c ab ac bc abc

Actual output :

a b c ab bc abc

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • 1
    On my computer, your program printed "a ab abc b bc c" (with newlines). Are you sure you run exactly this program and receive exactly this output? – user31264 Dec 04 '17 at 19:40
  • 1
    You expect `ac` but not `ad` or `acd`? – crashmstr Dec 04 '17 at 19:41
  • 2
    "ac" is not a substring of "abc", that's why it didn't print it. – user31264 Dec 04 '17 at 19:42
  • @crashmstr The snippet actually only uses `"abc"` as input, contrary to the question. I've edited to question to correct the error. – François Andrieux Dec 04 '17 at 19:43
  • @JTejedor- It will print duplicate also. – chandra Deep Dec 04 '17 at 19:57
  • @user31264: is it possible to print all the different strings from the given string character? – chandra Deep Dec 04 '17 at 20:01
  • 1
    `#include using namespace std;` is a really risky combination. The first includes the entire C++ standard library (in g++ anyway, Every other compiler I'm aware of will report an error). The second places the entire standard library into the global namespace where extra the tens of thousands of identifiers will compete with identifiers you declare and can wreak havoc. – user4581301 Dec 04 '17 at 20:10
  • @user4581301: It's a bad habit spread by those terrible "online coding" games, where you get points for producing bad code faster or something like that. – Christian Hackl Dec 04 '17 at 21:09
  • @ChristianHackl Seen it at ACM tourneys as well. I think the competition runners should randomize the compilers or add a code review component. – user4581301 Dec 04 '17 at 21:16
  • @user4581301 I am not the OP, but I never saw that "using namespace std;" in .cpp file had really "wreak havoc". I only saw claims it can happen. These claims were repeated so many times that people take them for granted. – user31264 Dec 04 '17 at 21:49
  • @user31264 Seen it happen and done it to myself, though not lately. Usually on SO, it is someone writing a `reverse` or `swap` function and accidentally wind up calling the one in `std`. A while back someone got `std::pow` in place of `pow` and somehow `std::pow` wound up returning 24 for `pow(5,2)`. Why the result was different, I don't know, but this is more a case of don't send a double to do an integer's job. Anyway, I'm more a use with caution advocate, but pulling in the entire standard library is a bit extreme. The odds of pulling in something you don't know about is too high. – user4581301 Dec 04 '17 at 22:04

1 Answers1

-2

Please see the solution below:

#include <iostream>

using namespace std;

int main()
{
    string input = "abc";
    for (int length = 0; length < input.length(); length++) {
        for (int offset = 0; offset <= input.length() - length; offset++) {
            cout << input.substr(offset, length) + "\n";
        }
    }
    cout << input;

    return 0;
}
bamberjp
  • 182
  • 4
  • The problem is not a substring problem. Any answer with substr() is almost certainly wrong, and this one certainly – MSalters Dec 04 '17 at 21:50