-5

it's requested that a given string char input_str[5] = "Hello"; must be padded with leading character = 0. so the result will have a fixed length of 10 characters: out_put == "00000Hello".

any idea how to do so in C?

1 Answers1

1

here you are an example

int targetStrLen = 10;           // Target output length  
const char *myString="Hello";   // String for output 
const char *padding="0000000000000000000000000000000000000000000000000000000000000";

int padLen = targetStrLen - strlen(myString); // Calc Padding length
if(padLen < 0) padLen = 0;    // Avoid negative length

printf("[%*.*s%s]", padLen, padLen, padding, myString);  // LEFT Padding 
printf("[%s%*.*s]", myString, padLen, padLen, padding);  // RIGHT Padding 

If you have some doubt see this answer https://stackoverflow.com/a/9741091/3284537 where I extract the code

Emiliano
  • 669
  • 8
  • 26