How to print only certain parts of a string?

后端 未结 2 1240
终归单人心
终归单人心 2021-01-12 08:34

I have a string const char[15] and I want to print it like this:

Label-one: characters [0,13)
Label-two: characters [

2条回答
  •  不知归路
    2021-01-12 09:30

    printf("Label-one: %.*s", 13, str);
    printf("Label-two: %.*s", 2, str + 13);
    

    @Bob's answer is also acceptable if these lengths are constant, but in case the lengths are determined at runtime, this is the best approach since it parametrises them.

提交回复
热议问题