Padding specified number of zeros to a char array

c++ pad string with zeros
c pad string with leading zeros
printf pad string with zeros
memset char array
printf right pad
zero padding in c
string padding in c

I want to pad a given char array to make it a 15 character array.

For eg. if the array contains two characters 1, 2 then 13 0 characters should be padded to make in 000000000000012 and if contains five characters then 10 0s should be padded. The resultant array should contain 15 characters always.

Found one solution here but that’s for stl string I need similar solution for char arrays. Please help. What I have tried is below:

char moneyArray[256];
memset(moneyArray, 0, 256);    
for(int i=0;i<(15-strlen(moneyArray))-1;i++)

But I am looking for a standard solution if possible using a std function may be?

If you're fine with std::string (and I think you should be), you can make use of its fill constructor:

char        s[]    = "12";
std::string padded = std::string( (15 - strlen(s) ), '0').append(s);

Of course you might want to check whether strlen(s) > 15 first.

Pad a character array with n zeros - MATLAB Answers, Is there a quick way where I can have that number of zeros needed (10-numel(​num)) padded on the front? PS - I love the de2bi function where you can specify the  According to the manual, i'll need to pad the piece with zeros until it reaches the length of 16 byte. Well, this is my problem: I can't pad with \0 since C cuts this

You can use the pad function below:

#include <iostream>
#include <cstring>

void pad(char *s, int n, int c) {
    char *p = s + n - strlen(s);
    strcpy(p, s);
    while (p >= s) { p[0] = c; p--; }

int main () { 
    char b[16] = "123";
    pad(b, 15, '0');
    std::cout << b << std::endl;
    return 0; 

c++ - Padding specified number of zeros to a char array -, i want pad given char array make 15 character array. for eg. if array contains 2 characters 1, 2 13 0 characters should padded make in 000000000000012 , if  B = padarray(A,padsize) pads array A with an amount of padding in each dimension specified by padsize. The padarray function pads numeric or logical images with the value 0 and categorical images with the category <undefined> .

You have various options; one of them would be (again under the assumption we already know that moneyArray contains a string and is a 16-byte buffer at least):

size_t len = strlen(moneyArray); 
memmove(moneyArray + 15 - len, moneyArray, len + 1);
memset(moneyArray, '0', 15 - len);

4.1. Padding a String - C++ Cookbook [Book], 4.1. Padding a String Problem You need to “pad,” or fill, a string with a number of occurrences of some character to a certain width. For example, you may want  Note: padding a number with zeros this way changes the number into text, which may or may not suit your needs. To simply display a number with padding, see below. Pad for display only. To pad a number with zeros for display only, just select the cell(s) and apply a custom number format like so: Format cells > Number > Custom > "00000"

Pad an integer with leading zeros in C++, Objective. To pad an integer with leading zeros to a given minimum width when converting it to a character string in C++. Scenario. Suppose you are writing  The replicate T-SQL function in Microsoft’s SQL Server makes it simple to add leading zeros to a number value. Let’s create a table ‘emp’ with empid and salary columns. create table emp ( empid int, salary int ); Insert some sample data into table ‘emp’. insert into emp values (1,300); insert into emp values […]

how can I convert int to zero padded char array, Jul 19, 2016, 07:38 pm. Given an integer like 791. I want to convert that to a zero padded char array of five characters like "00791" How can I do that? Charlie R  The above approach does not use any external library to pad string. We first stored the required string length and pad characters. For padding left with zeros, we first took a substring from the padding character string starting from index equal to the string size we want to pad, which gave us (required length – string length) pad characters.

Java 6 Programming Black Book, New Ed, Method original, int new Length) Does this characters (if necessary) so the int new Length) It copies the specified array, truncating or padding with zeros (if two given arrays of bytes are equal to one another static boolean equals (char [] a​,  Most of the time in my code, I'm not lucky enough to deal with a single number like 1 in every instance. I want to add a range of zeroes because the number I'm dealing with could be 1, 513, or 2303. I need a way to add the correct number of leading zeroes dynamically. Perhaps I want to add leading zeroes up to a maximum of two.

  • when you say padded with zeros, what exactly do you mean by zeros?
  • zeros mean characters 0 for e.g. 12 becomes 000000000000012
  • OK, maybe specify that you mean '0', not 0 in the question.
  • you can try with sprintf(); here's a thread on that: