Your Own Version of left-pad()

Difficulty: ★ ★ ☆ ☆

Padding a string on its left side isn’t that difficult, yet it’s a vital piece of code. To understand why, you need to know that the initialism NPM stands for Node Package Manager. It’s a critical part of the Internet.

The NPM contains thousands of Javascript modules. It works as a database, providing building blocks that allow coders to quickly craft programs without having to re-invent the wheel.

Most of the modules contained in the NPM are simple, doing those humble things required by any typical program. The modules build upon each other, creating a complex system of dependencies. This Jenga-tower-like vulnerability was exploited when the developer of the left-pad module, which adds spaces or other characters to the left side of a string, got upset and removed his code.

You can read the left-pad story here. The event was catastrophic, so it’s well-documented.

Destroying the NPM is not your task for this month’s Exercise.

No, your job is to write a left-pad function in C. Because C doesn’t allow hyphens in names (the compiler believes you’re subtracting something), you must name your function leftpad(). Here is the prototype:

char *leftpad(char *s, int width);

The first argument is a pointer to an existing string. The second is the pad size, the number of spaces to add before the string. The function returns the resulting string, which is the same as string s, but with width spaces on its left side.

A second version of the left-pad function is also available in the NPM, one that lets you set the padding character. For this exercise, assume that the character is a space.

To assist you, I offer the following skeleton to start:

#include <stdio.h>

char *leftpad(char *s, int width)
{
}

int main()
{
    const int count=5;
    char test_string[]="string";
    int pad[count] = { 5, 10, 0, -3, 1 };
    int x;

    for( x=0; x<count; x++ )
        printf("Pad %3d = %s\n",pad[x],leftpad(test_string,pad[x]) );

    return(0);
}

The main() function provides a sample string (test_string[]) and an integer array of various padding values to test. The for loop plows through each variation, outputting the resulting call to the leftpad() function.

Here’s a sample run of my solution:

Pad  5 =      string
Pad 10 =           string
Pad  0 = string
Pad -3 = string
Pad  1 =  string

This exercise isn’t particularly difficult. In fact, you can click here to see the Javascript version of left-pad as it currently exists in the NMP. But please try writing your own function rather than cribbing from Javascript. Click here to view my solution.

2 thoughts on “Your Own Version of left-pad()

  1. I think in padding functions the width argument is usually the total width to pad to, then if you want to use the function to right-align various strings in a single column you can then just pass the column width without having to subtract the string size to calculate the number of spaces.

  2. And that’s exactly how I would think it works, but for left-pad() in the NPM, it works the other way.

Leave a Reply