4

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++)
    sprintf(moneyArray,"0%s",moneyArray);

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

Community
  • 1
  • 1
Maven
  • 14,587
  • 42
  • 113
  • 174

4 Answers4

3

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);
    p--;
    while (p >= s) { p[0] = c; p--; }
}

int main () { 
    char b[16] = "123";
    pad(b, 15, '0');
    std::cout << b << std::endl;
    return 0; 
}
perreal
  • 94,503
  • 21
  • 155
  • 181
2

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.

Biffen
  • 6,249
  • 6
  • 28
  • 36
1

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);
M.M
  • 138,810
  • 21
  • 208
  • 365
0

you could just write code to move the chars up

char str[10] = "123456";
padStart(str, 7, '0');

str would become "0123456". be sure the char array is large enough to fit the longer string

void padStart(char *str, int len, char padChar)
{
    // find the null terminator
    int strLen = 0;
    while (str[strLen] != '\0')
    {
        strLen++;
    };
    
    // is there anything to actually do
    if (strLen < len)
    {       
        // move the string up to the given length
        for (int i = 0; i <= strLen; i++) // notice the '<=' to include the \0 terminator
        {
            str[len - i] = str[strLen - i];
        }
        
        // add padChar to the start
        for (int i = 0; i < len - strLen; i++)
        {
            str[i] = padChar;
        }
    }
}
Jay Dee
  • 143
  • 10
  • quite complicated for a memmove and a memset – OznOg Nov 07 '21 at 10:32
  • guess so, but i landed here trying to answer the same question but capable of running on an 8 bit microcontroller and non of the above compiled as i was missing some library or other, so i just wrote my own implementation that didn't need to call a library. Looking back now i prob could have used memmove and just wrote an implementation of strlen, but hay, it did what i needed it to do – Jay Dee Nov 08 '21 at 00:49
  • well if you don't have strlen nor memset, poor you! That said, you should mention this in your answer because as is, it looks really complicated for no reason. – OznOg Nov 08 '21 at 10:31
  • possibly personal opinion but i would say an answer that you can see what it will do line by line is far more use and less complicated than calling any function (standard or not) that your relying on some un-known non-visible code somewhere that someone else wrote to do what you need done. though i would probably accept that this approach may be less efficient – Jay Dee Nov 08 '21 at 10:50