This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# Generate all possible substrings

Hunter McMillen
Ranch Hand
Posts: 492
Hey everyone,

This is an assignment that I had last semester when I took my advanced programming class, but I was unable to figure it out. I stumbled across it this morning and was wondering if someone can give me some input on what's going wrong.

We were supposed to used a recursive method to generate all possible substrings of a given string without repeats.

Here's the code:

If I pass in cat into my program I get:
cat
ca
c
a
at
a
t

I'm not really sure where the repeated a is coming from.

Hunter.

Uli Hofstoetter
Ranch Hand
Posts: 57
Hunter McMillen wrote:Hey everyone,

This is an assignment that I had last semester when I took my advanced programming class, but I was unable to figure it out. I stumbled across it this morning and was wondering if someone can give me some input on what's going wrong.

If I pass in cat into my program I get:
cat
ca
c
a
at
a
t

I'm not really sure where the repeated a is coming from.

Hunter.

I did not really look at the code, but it looks like the substrings "ca" and "at" are extracted from cat.

Then "ca" is further divided into "c" and "a" as well as "at" is further divided into "a" and "t".

fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
not printing duplicates could get hard. imagine if your input string is "the fat cat sat on a rat". it's easy to see how you might get "at" a lot...

What I'd probably do is generate each one like you are, but instead of printing it, insert it into a set.

Then, after you are all done generating them, iterate over your set elements to print them out.

Hunter McMillen
Ranch Hand
Posts: 492
Thanks for the help guys, i figured it out.

This is what I changed it to:

Hunter.

Campbell Ritchie
Sheriff
Posts: 48394
56
Well done

Piet Verdriet
Ranch Hand
Posts: 266
Campbell Ritchie wrote:Well done

Seconded!