String Padding in C

前端 未结 10 1547
迷失自我
迷失自我 2020-11-28 22:25

I wrote this function that\'s supposed to do StringPadRight(\"Hello\", 10, \"0\") -> \"Hello00000\".

char *StringPadRight(char *string, int padded_len, char          


        
10条回答
  •  感动是毒
    2020-11-28 22:56

    #include 
    #include 
    
    int main(void) {
        char buf[BUFSIZ] = { 0 };
        char str[] = "Hello";
        char fill = '#';
        int width = 20; /* or whatever you need but less than BUFSIZ ;) */
    
        printf("%s%s\n", (char*)memset(buf, fill, width - strlen(str)), str);
    
        return 0;
    }
    

    Output:

    $ gcc -Wall -ansi -pedantic padding.c
    $ ./a.out 
    ###############Hello
    

提交回复
热议问题