Print out all possible sizes of subsequences of string in C For example, given a string "abcdefghijk", I want my function to print out:


a, b, c, d, e, f, g, h, i, j, k.

ab, bc, cd, de, ef, fg, gh, hi, ij, jk

abc, bcd, cde, def, efg, fgh, ghi, hij, ijk

abcd, bcde



I'm sure there's a better way to do this than what I currently have. What are some optimizations I could do? Mostly in terms of speed performance, but any kind of optimization would be great; time, space, design, etc...

The commas don't matter much, it's the order that I worry about at this point, so that a, b, b, c, c, d, d, e, e, f, f, g, g, h, h, i, i, j, j, k is fine for the second line.

int main(int argc, char const *argv[])
{

    char* s = "abcdefghijk";
    int n = 11;

    for (int k = 0; k &lt; n; k++)
    {
        for (int i = 0; i &lt; n-k; i++)
        {           
            for (int j = i; j &lt;= i+k; j++)
                printf("%c, ",s[j]);
        }
        printf("\n");
    }


    return 0;
}
Here's a way that (at least IMO) at least marginally cleaner:

#include &lt;stdio.h&gt;

int main(int argc, char const *argv[])
{

    char s[] = "abcdefghijk";
    int n = sizeof(s);

    for (int k = 1; k &lt; n; k++)
    {
        for (int i = 0; i &lt; n - k; i++)
        {
            printf("%*.*s, ", k, k, s + i);
        }
        printf("\n");
    }

    return 0;
}


As to things about the code that can be improved, the most obvious would be using sizeof (or strlen, if necessary) to find the length of the input array, instead of hard-coding it.

Realistically, there's probably not a lot you can do to improve speed/performance--it's virtually guaranteed to be heavily I/O bound (though Tom Ernack's suggestion of using fwrite instead of printf might help--then again, depending on the standard library you're using, it might not).