Add space to a string

后端 未结 2 1417
误落风尘
误落风尘 2021-01-21 04:41

I am trying to add a space to each space until column = 0. I am not sure how to do this.

The problem is the following. If you look at a newspaper you will s

2条回答
  •  没有蜡笔的小新
    2021-01-21 04:59

    #include 
    #include 
    #include 
    
    #define WIDTH 70
    #define _(x) #x
    #define str(x) _(x)
    
    void ruler_print(int n){
        char ruler[] = "1234567890";
    
        while(n>9){
            printf(ruler);
            n -= 10;
        }
        ruler[n] = '\0';
        printf("%s\n", ruler);
    }
    
    int count_word(const char *text, size_t *count_char){
        int i;
        char *wk, *p;
        p=wk=strdup(text);
        *count_char=0;
        for(i=0;p=strtok(p, " ");++i,p=NULL){
            *count_char+=strlen(p);
        }
        free(wk);
        return i;
    }
    
    int main(void){
        int column, len, word_count;
        int i, spaces, between, remain;
        size_t count_char;
        char text[WIDTH + 1];
        char *p = text;
    
        printf("Enter the width of the column: ");scanf("%d%*c", &column);
        printf("Enter a line of text: ");scanf("%" str(WIDTH) "[^\n]", text);
        len=strlen(text);
        if(len > column || len > WIDTH){
            fprintf(stderr, "too long text!\n");
            return -1;
        }
        ruler_print(WIDTH);
        word_count = count_word(text, &count_char);
        spaces = column - count_char;
        between = spaces / (word_count -1);
        remain = spaces - (word_count -1)*between;
        strtok(text, " ");
        for(i=0;i

提交回复
热议问题