Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Printing certain subsequences of a given string in C

I want to create a program in C which prints subsequences as follows:

If the user types "Lais", the program is supposed to print

L
a
i
s
La
Li
Ls
ai
as
is
Lai
Las
Lis
ais
Lais

So far, what I have is the following:

#include <stdio.h>
#include <string.h>

int factorial(int n)
{
    if (n == 0)
        return 1;
    else
        return(n*factorial(n - 1));
}

int main(void)
{
    char s[100];
    int n, i, j, k;
    scanf("%s", s);
    n = strlen(s);
    for (i = 0; i < n + 1; i = i + 1)
    {
        for (j = 0; j < (factorial(n) / factorial(i) / factorial(n - i)); j = j + 1)
        {
            for (k = 0; k < i; k = k + 1)
            {
                printf("%c", s[? ? ? ]);
            }
            printf("\n");
        }
    }
}

But I don't have a good idea as to what to put in place of the question marks. It's supposed to be a formula on i,j and k. Alternatively, if there is a different, (hopefully) simpler way to do things, I appreciate any tips. Specially if this can be done using recursion in the main function.

Thanks in advance.

Edit:

The program runs fine. For instance, as it stands, if I substitute the question marks by k, the program outputs:

L
L
L
L
La
La
La
La
La
La
Lai
Lai
Lai
Lai
Lais

What this means is that the indexes i,j,k are counting things correctly in the sense that the program is printing 1,4,6,4,1 lines with 0,1,2,3,4 letters each, respectively, which is what it is supposed to happen (modulo the first empty line, but that's a problem for later).

Of course, the problem is that if I just put a k as a formula in place of the question marks, the thing just prints the letters in order, which is obviously not what I want. The formula is going to have to depend on i,j,k. Hope this clarify things.

Comments