String Padding in C

前端 未结 10 1542
迷失自我
迷失自我 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 23:07

    You must make sure that the input string has enough space to hold all the padding characters. Try this:

    char hello[11] = "Hello";
    StringPadRight(hello, 10, "0");
    

    Note that I allocated 11 bytes for the hello string to account for the null terminator at the end.

提交回复
热议问题