{"id":5422,"date":"2022-07-08T00:01:55","date_gmt":"2022-07-08T07:01:55","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5422"},"modified":"2022-07-02T10:26:57","modified_gmt":"2022-07-02T17:26:57","slug":"your-own-version-of-left-pad-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5422","title":{"rendered":"Your Own Version of <em>left-pad()<\/em> &#8211; Solution"},"content":{"rendered":"<p>Can you write your own <em>left-pad<\/em> function in C? Would you get so angry that you&#8217;d pull it from the Jenga-tower NPM and bring the Internet to its knees? I hope your answer <em>Yes<\/em> to the first question and <em>No<\/em> to the second, because your task for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5420\">this month&#8217;s Exercise<\/a> is to write that function.<br \/>\n<!--more--><br \/>\nAdding spaces to the left side of a string is simple to code, though many potential solutions exist. If the program were just outputting the spaces and original string, it would be even easier. Alas, this solution requires that you create and return a new string from the <em>leftpad()<\/em> function. Here is my version:<\/p>\n<pre class=\"screen\">\r\nchar *leftpad(char *s, int width)\r\n{\r\n    int size,x;\r\n    char *buffer;\r\n    char *b;\r\n\r\n    <span class=\"comments\">\/* calculate the final string size *\/<\/span>\r\n    size = strlen(s) + width;\r\n    \r\n    <span class=\"comments\">\/* allocate storage for the buffer *\/<\/span>\r\n    buffer = malloc( sizeof(char) * size + 1 );\r\n    if( buffer==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to allocate memory\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* pad the left end with spaces *\/<\/span>\r\n    b = buffer;\r\n    for( x=0; x&lt;width; x++ )\r\n    {\r\n        *b = ' ';\r\n        b++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* append the string *\/<\/span>\r\n    strcpy(b,s);\r\n\r\n    <span class=\"comments\">\/* return the result *\/<\/span>\r\n    return(buffer);\r\n}<\/pre>\n<p>I use <em>char<\/em> pointer <code>buffer<\/code> to hold the final string. Pointer <code>b<\/code> is an index that transfers the original string (argument <code>s<\/code>) into <code>buffer<\/code>.<\/p>\n<p>First, storage is allocated for <code>buffer<\/code>. Its size is based on the original string&#8217;s length plus the width value, plus one for the null character. I use variable <code>size<\/code> to hold the string&#8217;s new length (minus the null character) just to make the code more readable.<\/p>\n<p>Second, the buffer is padded with <code>width<\/code> number of spaces. A <em>for<\/em> loop uses the <code>width<\/code> value and variable <code>b<\/code> tracks the offset within <code>buffer<\/code>. The spaces are plopped into the newly created string.<\/p>\n<p>Third, the <em>strcpy()<\/em> function copies the passed string <code>s<\/code> to the location set in variable <code>b<\/code>, which is within <code>buffer<\/code>. The <em>strcopy()<\/em> function handles terminating the string, which is ready to return to the caller, nicely padded.<\/p>\n<p>You can <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_07-Exercise.c\" rel=\"noopener\" target=\"_blank\">click here<\/a> to view my full solution. Here is sample output:<\/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>My solution&#8217;s approach is different from what is shown online in the NPM. Your&#8217;s may differ as well, given that multiple solutions exist for various programming puzzles. I hope that you met with success, and that you no longer have plans to take down the Internet.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Can you write your own left-pad function in C? Would you get so angry that you&#8217;d pull it from the Jenga-tower NPM and bring the Internet to its knees? I hope your answer Yes to the first question and No &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5422\">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":[5],"tags":[],"class_list":["post-5422","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5422","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=5422"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5422\/revisions"}],"predecessor-version":[{"id":5448,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5422\/revisions\/5448"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}