{"id":2430,"date":"2017-04-01T00:01:51","date_gmt":"2017-04-01T07:01:51","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2430"},"modified":"2017-03-25T09:46:02","modified_gmt":"2017-03-25T16:46:02","slug":"reducing-roman-numerals","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2430","title":{"rendered":"Reducing Roman Numerals"},"content":{"rendered":"<p>I&#8217;m sure a mathematical solution exists to condense Roman numeral values. The values ending in 4 or 9 can be reduced, but whatever that solution is, it&#8217;s beyond me. So for my resolution to the Roman numeral reduction problem, I rely on strings instead of math.<br \/>\n<!--more--><br \/>\nAssuming that the original Roman numeral is ordered high-to-low, and that no value is out of sequence, then only six character sequences (strings) exist that can be reduced:<\/p>\n<p>DCCCC to CM<br \/>\nCCCC to CD<br \/>\nLXXXX to XC<br \/>\nXXXX to XL<br \/>\nVIIII to IX<br \/>\nIIII to IV<\/p>\n<p>In each of these reductions, repeating values of four characters are present: CCCC, XXXX, and IIII. I tried to craft a function that looked for those strings, but in the end it was easier to do a search for all six strings and replace them with their reduced counterparts &mdash; a search-and-replace.<\/p>\n<p>The function I wrote is called <em>rn_srink()<\/em> and it requires a pointer to an existing Roman numeral string. The function searches for one of the six instances shown above and replaces it with the reduced characters. The string must be dissected and then reassembled to complete this task. Because pointers are used, special care is required to ensure that the memory locations don&#8217;t get lost; it&#8217;s difficult to manipulate a pointer passed to a function (<a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2288\">Lesson\/Series<\/a>), so I created a local copy of the string for manipulation.<\/p>\n<p>Here&#8217;s the <em>rn_shrink()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nvoid rn_shrink(char *r)\r\n{\r\n    char *change_from[6] = {\r\n        \"DCCCC\", \"CCCC\", \"LXXXX\", \"XXXX\", \"VIIII\", \"IIII\"\r\n    };\r\n    char *change_to[6] = {\r\n        \"CM\", \"CD\", \"XC\", \"XL\", \"IX\", \"IV\"\r\n    };\r\n    char new_r[SIZE];\r\n    char *base,*rest;\r\n    int x;\r\n\r\n    for(x=0;x&lt;6;x++)\r\n    {\r\n        base = strstr(r,change_from[x]);\r\n        if( base != NULL)\r\n        {\r\n            rest = base+strlen(change_from[x]);\r\n            if( base == r)\r\n            {\r\n                strcpy(new_r,change_to[x]);\r\n            }\r\n            else\r\n            {\r\n                *base = '\\0';\r\n                strcpy(new_r,r);\r\n                strcat(new_r,change_to[x]);\r\n            }\r\n            if( *rest != '\\0')\r\n                strcat(new_r,rest);\r\n            strcpy(r,new_r);\r\n        }\r\n    }\r\n}<\/pre>\n<p>The <em>for<\/em> loop processes the string six times, one for each element in the <code>change_from[]<\/code> and <code>change_to[]<\/code> arrays. The <em>strstr()<\/em> function attempts to locate the string. If found, its location is saved in the <code>rest<\/code> pointer.<\/p>\n<p>The <code>if( base == r)<\/code> test is true when the search text (long value) is found at the start of the string. In that case, the replacement string (short value) is copied from <code>change_to[]<\/code> into the <code>new_r<\/code> buffer. Otherwise, the first part of the original string <code>r<\/code> is copied (<em>strcpy()<\/em>)to the <code>new_r<\/code> buffer. Otherwise, the reduced string from <code>change_to[]<\/code> is appended to <code>new_r<\/code> by using the <em>strcat()<\/em> function. Finally, the <code>if( *rest != '\\0')<\/code> test ensures that the replacement didn&#8217;t occur at the end of the string. If not, the rest of the original string is appended to <code>new_r<\/code> by using another <em>strcat()<\/em> function. Figure 1 illustrates this operation.<\/p>\n<div id=\"attachment_2440\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2440\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/04\/0401-figure1.png\" alt=\"\" width=\"550\" height=\"261\" class=\"size-full wp-image-2440\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/04\/0401-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/04\/0401-figure1-300x142.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/04\/0401-figure1-500x237.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-2440\" class=\"wp-caption-text\">Figure 1. How the find, slice, and build operation works.<\/p><\/div>\n<p>The <em>rn_shink()<\/em> function is added code presented in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2424\">last week&#8217;s Lesson<\/a>. To view the full code, <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/04\/0401.c\">click here<\/a>.<\/p>\n<p>Here are some sample runs:<\/p>\n<pre><code>Type an integer value: 2999\r\nThe value 2999 is MMDCCCCLXXXXVIIII\r\nShortened value is MMCMXCIX\r\n\r\nType an integer value: 1974\r\nThe value 1974 is MDCCCCLXXIIII\r\nShortened value is MCMLXXIV<\/code><\/pre>\n<p>I feel that this solution is rather clunky; I&#8217;d much rather find a mathematical solution and simply rebuild the string from scratch, but like many programs, this one works so I&#8217;m good with it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spare no digit to condense values such as IIII to IV and DCCCC to CM. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2430\">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":[2],"tags":[],"class_list":["post-2430","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2430","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=2430"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2430\/revisions"}],"predecessor-version":[{"id":2455,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2430\/revisions\/2455"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}