{"id":2737,"date":"2017-10-08T00:01:40","date_gmt":"2017-10-08T07:01:40","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2737"},"modified":"2017-10-07T11:09:53","modified_gmt":"2017-10-07T18:09:53","slug":"right-string-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2737","title":{"rendered":"Right String &#8211; Solution"},"content":{"rendered":"<p>For <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2719\">this month&#8217;s Exercise<\/a>, I offer both a string function solution as well as a solution that uses pointers. As you can guess, I thought of the string method first, which is where most programmers would run to avoid the dratted topic of pointers.<br \/>\n<!--more--><br \/>\nThe same <em>right()<\/em> function declaration is used for both examples:<\/p>\n<p><code>char *right(char *string, int size)<\/code><\/p>\n<p>The text <code>string<\/code> is passed to the function. Variable <code>size<\/code> determines how many characters from the right side of <code>string<\/code> are returned, though technically a pointer to the start of that chunk is what&#8217;s returned.<\/p>\n<p>For my non-pointer version, I crafted the following <em>right()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nchar *right(char *string, int size)\r\n{\r\n    int len;\r\n\r\n    len = strlen(string);\r\n    return(string+len-size-1);\r\n}<\/pre>\n<p>Variable <code>len<\/code> obtains the string&#8217;s length via the <em>strlen()<\/em> function. To get the position of the rightmost <code>size<\/code> characters, I use the calculation <code>string+len-size-1<\/code>. In this expression, <code>string+len<\/code> calculates the end of the string, where the null character (<code>'\\0'<\/code>) dwells. Subtract the value of <code>size<\/code>, plus 1 for the null character, to find the starting location of the rightmost <code>size<\/code> characters in <code>string<\/code>. That location (a pointer) is returned.<\/p>\n<p>The pointer variation on the <em>right()<\/em> function doesn&#8217;t require the <em>strlen()<\/em> function. Instead, I loop through the string to find the null character:<\/p>\n<pre class=\"screen\">\r\nchar *right(char *string, int size)\r\n{\r\n    while(*string++)\r\n        ;\r\n\r\n    return(string-size-2);\r\n}<\/pre>\n<p>The <em>while<\/em> loop processes the string for each non-null character. It stops when <code>*string=='\\0'<\/code>, but because of the <code>++<\/code> postfix, the pointer references the memory location <em>after<\/em> the <code>'\\0'<\/code>.<\/p>\n<p>In the <em>return<\/em> statement, the value of <code>size<\/code> is subtracted from <code>string<\/code>&#8216;s memory location. Because <code>string<\/code> points at two characters beyond its end (the null character and whatever character follows it), the value 2 is subtracted.<\/p>\n<p><strong>WARNING:<\/strong> This variation is risky because <code>string<\/code> references a memory location beyond what&#8217;s allocated. A segmentation fault or similar error could generate at runtime should the memory location exist outside the program&#8217;s control.<\/p>\n<p>Both functions yield the same output in my sample code:<\/p>\n<pre><code>http:\/\/c-for-dummies.com\/images\/c.png -&gt; c.png\r\nhttp:\/\/www.c-for-dummies.com\/images\/c.png -&gt; c.png\r\n\/c-for-dummies.com\/images\/c.png -&gt; c.png<\/code><\/pre>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/09\/10exercise-a.c\">Click here<\/a> to view the first, string solution.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/09\/10exercise-b.c\">Click here<\/a> to view the second, pointer solution.<\/p>\n<p>If you came up with a different solution, wonderful! Ensure that you test your function to ensure that the rightmost <em>n<\/em> characters of the string are returned. I ran my functions through the paces, and they worked fine &mdash; just like that old BASIC language RIGHT$ function from long ago.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For this month&#8217;s Exercise, I offer both a string function solution as well as a solution that uses pointers. As you can guess, I thought of the string method first, which is where most programmers would run to avoid the &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2737\">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-2737","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\/2737","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=2737"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2737\/revisions"}],"predecessor-version":[{"id":2766,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2737\/revisions\/2766"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}