{"id":3665,"date":"2019-07-08T00:01:16","date_gmt":"2019-07-08T07:01:16","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3665"},"modified":"2019-07-06T10:06:26","modified_gmt":"2019-07-06T17:06:26","slug":"write-your-own-integer-conversion-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3665","title":{"rendered":"Write Your Own Integer Conversion &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3656\">Exercise<\/a> is to emulate the antique <em>atoi()<\/em> function, which converts a string of digits into an integer value.<br \/>\n<!--more--><br \/>\nOne of the built-in frustrations of this type of conversion is that strings are read left-to-right and values are interpreted right-to-left. I didn&#8217;t initially see the brilliance of a left-to-right solution, so for my first solution I crafted the <em>convert()<\/em> function to process the string backwards in a <em>for<\/em> loop:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint convert(char *s)\r\n{\r\n    int len,x,r;\r\n\r\n    len = strlen(s);\r\n    for( x=0, r=0; x&lt;len; x++ )\r\n    {\r\n        r += ( *(s+len-x-1) - '0' ) * pow( 10.0, (double)x );\r\n    }\r\n\r\n    return(r);\r\n}\r\n\r\nint main()\r\n{\r\n    char value[] = \"345966\";\r\n    int v;\r\n\r\n    v = atoi(value);\r\n    printf(\"%s from atoi() is %d\\n\",value,v);\r\n    v = convert(value);\r\n    printf(\"%s from convert() is %d\\n\",value,v);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop at Line 11 has two initializations, <code>x<\/code> the counting variable and <code>r<\/code> the total to return from the function.<\/p>\n<p>The complex evaluation at Line 13 increases the value of variable <code>r<\/code> first by the left-most digit in the string, calculated as <code>*(s+len-x-1)<\/code>. The value <code>'0'<\/code> is subtracted from the character&#8217;s ASCII value, yielding an integer. This result is raised to the power of 10 based on the value of <code>x<\/code>: <code>pow(10.0,(double)x)<\/code>. The effect is that the integer&#8217;s value is increased for each position right-to-left in the string.<\/p>\n<p>Honestly, I must have been seriously nerdy when I thought of this solution. Yet, for all its clunky math, it works. Here&#8217;s the output:<\/p>\n<p><code>345966 is 345966<br \/>\n345966 is 345966<\/code><\/p>\n<p>For my second solution, I fought to process the string left-to-right. I had a similar notion about how the math would work, but the result turned out to be far more elegant than my first stab at a solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint convert(char *s)\r\n{\r\n    int r = 0;\r\n\r\n    while( *s )\r\n    {\r\n        r = (r*10) + (*s-'0');\r\n        s++;\r\n    }\r\n\r\n    return(r);\r\n}\r\n\r\nint main()\r\n{\r\n    char value[] = \"345966\";\r\n    int v;\r\n\r\n    v = atoi(value);\r\n    printf(\"%s is %d\\n\",value,v);\r\n    v = convert(value);\r\n    printf(\"%s is %d\\n\",value,v);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The naked simplicity of Line 10 is appalling. The <em>while<\/em> loop processes the string left-to-right, variable <code>s<\/code> incremented at Line 11. For each iteration, the existing value of <code>r<\/code> is multiplied by 10 and the value of the current digit is added: <code>(*s='0')<\/code>. This process repeats until the string has been read and it yields a perfect result:<\/p>\n<p><code>345966 is 345966<br \/>\n345966 is 345966<\/code><\/p>\n<p>Because the string is processed left-to-right, I didn&#8217;t need to find churn through the entire string, find its end, or bring in the math library. By not over-thinking the solution, I came up with something sweet and elegant.<\/p>\n<p>I hope your solution was more like my second and less like my first. Still, both solutions work. If your solution was able to convert the string into the proper value, congratulations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise is to emulate the antique atoi() function, which converts a string of digits into an integer value.<\/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-3665","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\/3665","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=3665"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3665\/revisions"}],"predecessor-version":[{"id":3678,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3665\/revisions\/3678"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}