{"id":2719,"date":"2017-10-01T00:01:02","date_gmt":"2017-10-01T07:01:02","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2719"},"modified":"2017-10-09T08:12:06","modified_gmt":"2017-10-09T15:12:06","slug":"right-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2719","title":{"rendered":"Right String"},"content":{"rendered":"<p>A recent web page programming puzzle reminded me of an old BASIC language function, RIGHT$. The &#8220;right-string&#8221; function returns the rightmost <em>n<\/em> characters from a string. Such a function would have helped me greatly in my web page programming task.<br \/>\n<!--more--><br \/>\nThe problem I was trying to solve was to fetch a filename from a pathname. The filename is the right-most part of the pathname, preceded by a slash (or backslash in Windows). For example:<\/p>\n<p><code>http:\/\/c-for-dummies.com\/images\/c.png<\/code><\/p>\n<p>In the above web address (which is a fancy pathname) <code>c.png<\/code> is the filename I wanted. I could have coded a filename-extractor utility, one that scans a string for the last part, the filename, and generates that output. Instead, I recalled the old RIGHT$ function and decided to craft it instead. That&#8217;s because the filename I wanted was always going to be the same length; what changed was the address. For example:<\/p>\n<p><code>http:\/\/c-for-dummies.com\/images\/c.png<br \/>\nhttp:\/\/www.c-for-dummies.com\/images\/c.png<br \/>\n\/c-for-dummies.com\/images\/c.png<\/code><\/p>\n<p>Recalling from my youthful BASIC language programming days, if I used the RIGHT$ command on each of the above strings, I could easily extract the <code>c.png<\/code> filename. The format for RIGHT$ (in BASIC) is:<\/p>\n<p>RIGHT$(var$,n)<\/p>\n<p>where <em>var$<\/em> is a string variable or constant, and <em>n<\/em> is the number of characters to extract from the right end of <em>var$<\/em>.<\/p>\n<p>Your task for this month&#8217;s Exercise is to code a <em>right()<\/em> function. It takes two arguments, a string and a size, and returns a pointer to the position in string relative to value the string&#8217;s end (on the right side). For example:<\/p>\n<p><code>right(\"http:\/\/c-for-dummies.com\/images\/c.png\",4);<\/code><\/p>\n<p>returns the string <code>\"c.png\"<\/code>, or a pointer to that location within the string constant, shown above.<\/p>\n<p>You must process the three strings listed earlier and return the filename <code>c.png<\/code> as the result. Here&#8217;s sample output:<\/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>I can think of two ways to craft the <em>right()<\/em> function. <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2737\">Click here<\/a> to view my solutions. Before doing so, please try this Exercise on your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When the C language doesn&#8217;t offer the function you need, you write one yourself. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2719\">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-2719","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\/2719","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=2719"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2719\/revisions"}],"predecessor-version":[{"id":2769,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2719\/revisions\/2769"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}