I am trying to create programs to generate substrings of a string and also subsets of characters of a string.
For the substrings of the string "rum" there are seven: "r", "ru", "rum", "u", "um", "m", ""
For the subsets of the string "rum" there are eight: "r", "ru", "rum", "u", "um", "m", "rm", "".
So the only difference is "rm" in the second. I think I have created a program for the second one.
Here it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
using namespace std;
void subsets(int i, int j)
{
static char input[] = "rum";
static char output[sizeof input];
output[j] = '\0';
printf("%s\n", output);
while(input[i] != '\0') {
output[j] = input[i];
subsets(i + 1, j + 1);
do ++i;
while(input[i] == input[i - 1]);
}
}
int main()
{
cout << "The subsets of rum are: " << endl;
subsets(0, 0);
system("Pause");
return 0;
}
| |
I am confused on what to do for the first one because I feel it is so similar. Also, when I run my code, it skips a line for the first one. Does that mean it is the "" subset?
Thank you.