{"id":5420,"date":"2022-07-01T00:01:39","date_gmt":"2022-07-01T07:01:39","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5420"},"modified":"2022-07-09T07:00:09","modified_gmt":"2022-07-09T14:00:09","slug":"your-own-version-of-left-pad","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5420","title":{"rendered":"Your Own Version of <em>left-pad()<\/em>"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>Padding a string on its left side isn&#8217;t that difficult, yet it&#8217;s a vital piece of code. To understand why, you need to know that the initialism NPM stands for Node Package Manager. It&#8217;s a critical part of the Internet.<br \/>\n<!--more--><br \/>\nThe 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.<\/p>\n<p>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 <em>left-pad<\/em> module, which adds spaces or other characters to the left side of a string, got upset and removed his code.<\/p>\n<p>You can read the <a href=\"https:\/\/www.theverge.com\/2016\/3\/24\/11300840\/how-an-irate-developer-briefly-broke-javascript\" rel=\"noopener\" target=\"_blank\"><em>left-pad<\/em> story here<\/a>. The event was catastrophic, so it&#8217;s well-documented.<\/p>\n<p>Destroying the NPM is <em>not<\/em> your task for this month&#8217;s Exercise.<\/p>\n<p>No, your job is to write a <em>left-pad<\/em> function in C. Because C doesn&#8217;t allow hyphens in names (the compiler believes you&#8217;re subtracting something), you must name your function <em>leftpad()<\/em>. Here is the prototype:<\/p>\n<p><code>char *leftpad(char *s, int width);<\/code><\/p>\n<p>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 <code>s<\/code>, but with <code>width<\/code> spaces on its left side.<\/p>\n<p>A second version of the <em>left-pad<\/em> 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.<\/p>\n<p>To assist you, I offer the following skeleton to start:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nchar *leftpad(char *s, int width)\r\n{\r\n}\r\n\r\nint main()\r\n{\r\n    const int count=5;\r\n    char test_string[]=\"string\";\r\n    int pad[count] = { 5, 10, 0, -3, 1 };\r\n    int x;\r\n\r\n    for( x=0; x&lt;count; x++ )\r\n        printf(\"Pad %3d = %s\\n\",pad[x],leftpad(test_string,pad[x]) );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>main()<\/em> function provides a sample string (<code>test_string[]<\/code>) and an integer array of various padding values to test. The <em>for<\/em> loop plows through each variation, outputting the resulting call to the <em>leftpad()<\/em> function.<\/p>\n<p>Here&#8217;s a sample run of my solution:<\/p>\n<p><code>Pad&nbsp;&nbsp;5 =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string<br \/>\nPad&nbsp;10 =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string<br \/>\nPad&nbsp;&nbsp;0 = string<br \/>\nPad&nbsp;-3 = string<br \/>\nPad&nbsp;&nbsp;1 =&nbsp;&nbsp;string<\/code><\/p>\n<p>This exercise isn&#8217;t particularly difficult. In fact, you can <a href=\"https:\/\/www.npmjs.com\/package\/left-pad\" rel=\"noopener\" target=\"_blank\">click here<\/a> to see the Javascript version of <em>left-pad<\/em> as it currently exists in the NMP. But please try writing your own function rather than cribbing from Javascript. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5422\">Click here<\/a> to view my solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It seems like a silly piece of code, but it has a delicious and devious history. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5420\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-5420","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5420"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5420\/revisions"}],"predecessor-version":[{"id":5451,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5420\/revisions\/5451"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}