简体   繁体   中英

Inserting 'n' tabs in printf statement

I'm trying to understand recursion and backtracking with standard string permutation algorithm (swap --> permute --> swap)

Since I can't seem to get my head around how it backtracks, swaps the characters and permutes, I've tried to insert printfs at every recursive permute() call with incrementing indentation "levels". Anyway here's the code:

void permute(char arr[], int L, int R, int level)
{
    int i = 0;
    level = level+1;

    if(L == R)
    {
        printf("%*s", level, "\t");
        printf("printing : %s\n", arr);
    }
    else
    {
        for(i = L; i <= R; i++)
        {
            printf("%*s", level, "\t");
            printf("swapping %c with %c\n", arr[L], arr[i]);
            SWAP(&arr[L], &arr[i]);

            permute(arr, L+1, R, level);

            printf("%*s", level, "\t");
            printf("swapping back %c with %c\n", arr[i], arr[L]);
            SWAP(&arr[L], &arr[i]);
        }
    }
}
int main()
{
    char str[] = "abc";
    permute(str, 0, 2, 0);
    return 0;
}

What I want is printf("%*s", level, "\\t"); to put incrementing tabs as we go into deeper recursive levels. But that isn't working and I'm getting a block of text shifted by one tab.

swapping a with a
swapping b with b
printing : abc
swapping back b with b
swapping b with c
printing : acb
swapping back b with c
swapping back a with a
swapping a with b
swapping a with a
printing : bac
swapping back a with a
swapping a with c
printing : bca
swapping back a with c
swapping back a with b
swapping a with c
swapping b with b
printing : cba
swapping back b with b
swapping b with a
printing : cab
swapping back b with a
swapping back a with c

Any idea how to get printf to insert (level * '\\t') tabs?

The * in the format string isn't a repeat count; it is the minimum field width. If level is 19, the %*s will print 19 characters, but there's only one tab in the string, so it will always print just one tab and 18 spaces. The string will be right-justified (no - flag for left justification), so there'll be 18 spaces and a tab.

#include <stdio.h>

int main(void)
{
    int level = 19;
    printf("[%*s]\n", level, "\t");
    return 0;
}

You need a string of tabs such as "\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t" which is longer than the deepest level you'll go to. Or you need a tiny function to indent the appropriate number of tabs:

static inline void indent(int n)
{
    for (int i = 0; i < n; i++)
        putchar('\t');
}

and call that before calling printf() .

You could use a for loop: eg

for(int j = 0;j<level;j++) 
    printf("\t");

Easiest way:

#include <stdio.h>
    
int main (void)
{
    printf("how i do this\t");
    for(int i = 1 ; i <= 9; i++ ) {
        printf("    ");
    }
    printf("gotit");
    
    return 0;
}

Works how you want. You just have to put the first tab before the for part, so you are on the normal tab position and, because one tab is just for spaces, you can print 4 spaces as often as you want.

Just don't forget the amount you want - the first tab you did before, so here I wanted to set it on the 10th position after the text, so I stopped the loop after 9 times. If u want 1000, for example, just replace the 9 with 999 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM