{"id":3656,"date":"2019-07-01T00:01:35","date_gmt":"2019-07-01T07:01:35","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3656"},"modified":"2019-07-09T16:47:47","modified_gmt":"2019-07-09T23:47:47","slug":"write-your-own-integer-conversion","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3656","title":{"rendered":"Write Your Own Integer Conversion"},"content":{"rendered":"<p>It&#8217;s not that programmers are lazy. Many professions involve a lot of copy-and-paste, borrowing stuff done before, re-purposing and re-using. Why re-invent the wheel? Still, doing so helps exercise your programming kung-fu as well as offers insights into how things work and why some operations are done the way they are.<\/p>\n<p>As an example, consider the <em>atoi()<\/em> function.<br \/>\n<!--more--><br \/>\nAn older C language function, <em>atoi()<\/em> converts a string to an integer value. The name implies &#8220;ASCII to Integer.&#8221; Its current implementation is as a macro for the <em>strtol()<\/em> function. Both are prototyped in the <code>stdlib.h<\/code> header.<\/p>\n<p>Your task for this month&#8217;s Exercise is to write code that emulates the primary behavior of the <em>atoi()<\/em> function: convert a string value to an integer.<\/p>\n<p>Don&#8217;t worry about leading whitespace.<\/p>\n<p>Don&#8217;t fret over a sign before the value (positive or negative).<\/p>\n<p>And don&#8217;t frustrate yourself converting alphabetic characters.<\/p>\n<p>In fact, here&#8217;s the programming skeleton to get you started:<\/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    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>Your task is to complete the <em>convert()<\/em> function, which swallows a string value and returns a base 10 (decimal) integer. I can think of a number of ways to solve this Exercise. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3665\">Click here<\/a> to view my solutions, but please try devising your own solution before peeking at what I&#8217;ve done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sure, a function is available, but it&#8217;s good C programming practice to try writing your own. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3656\">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-3656","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\/3656","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=3656"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3656\/revisions"}],"predecessor-version":[{"id":3683,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3656\/revisions\/3683"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}