{"id":969,"date":"2014-11-01T00:01:33","date_gmt":"2014-11-01T07:01:33","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=969"},"modified":"2014-11-08T07:49:41","modified_gmt":"2014-11-08T15:49:41","slug":"from-text-to-integer","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=969","title":{"rendered":"From Text to Integer"},"content":{"rendered":"<p>A program prompts for a value. The user types &#8220;12345&#8221; at the keyboard, but that input is a string and not the value 12,345. Therefore, the code must convert the string into a value. This task can be accomplished in a number of ways.<br \/>\n<!--more--><br \/>\nThe easiest way to make the conversion is to use the <em>scanf()<\/em> function:<\/p>\n<p><code>scanf(\"%d\",&value);<\/code><\/p>\n<p>The <em>scanf()<\/em> function, however, won&#8217;t understand if the user screws up, potentially creating more work for you to code around that function&#8217;s peculiarities.<\/p>\n<p>Another option is to fetch a string and then process it through the <em>strtol()<\/em> function. That function, defined in <code>stdlib.h<\/code>, converts a string to a <em>long int<\/em> variable. It allows for white space before the value, a leading <code>+<\/code> or <code>-<\/code> sign, and it can read in hex values prefixed by using <code>0x<\/code> or octal values prefixed with a leading <code>0<\/code> (zero).<\/p>\n<p>The <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=137\"><em>strtol()<\/em> function<\/a> also terminates reading the string at the first non-digit character. It&#8217;s a vast improvement over the <em>atoi(<\/em>) function, which was the C library&#8217;s original &#8220;ASCII to integer&#8221; conversion function. (The <em>atoi()<\/em> function is also defined in <code>stdlib.h<\/code>.)<\/p>\n<p>Your challenge this month is to create your own text-to-integer function. I confess that this isn&#8217;t an easy exercise. It involves reading a string &#8212; a pointer &#8212; and using ASCII text conversion tricks to translate characters into values.<\/p>\n<p>To assist you, I&#8217;ve created a skeleton below that provides a starting point.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint convertString(char *string);\r\n\r\nint main()\r\n{\r\n    char *value = \"24680\";\r\n    int result;\r\n\r\n    result = convertString(value);\r\n    printf(\"The string %s represents value %d.\\n\",\r\n            value,\r\n            result);\r\n\r\n    return(0);\r\n}\r\n\r\nint convertString(char *string)\r\n{\r\n}<\/pre>\n<p>Your tasks is to complete the code by coding the <em>convertString()<\/em> function. You can get as fancy as you like, although the basic task is to create something that returns an <em>int<\/em> value from the string argument. Please don&#8217;t use <em>strtol()<\/em> or <em>atoi()<\/em> or another C library function. That&#8217;s cheating. You need to code your own function, which can be done!<\/p>\n<p>As a hint, it helps to know that the ASCII codes for characters &#8216;0&#8217; through &#8216;9&#8217; can be converted to values 0 through 9 by subtracting 0x30 (the code for character &#8216;0&#8217;). So, if you were converting single characters, you could do something like:<\/p>\n<pre><code>cvalue = digit - '0';<\/code><\/pre>\n<p>Where <code>cvalue<\/code> is an <em>int<\/em> variable and <code>digit<\/code> is a single character variable holding characters &#8216;0&#8217; through &#8216;9&#8217; inclusive. The result of the above operation would be that <code>cvalue<\/code> holds the integer value of character variable <code>digit<\/code>.<\/p>\n<p>I will post my <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1031\">solution<\/a> in a week.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write your own function to convert a text value to a numeric value. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=969\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-969","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\/969","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=969"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/969\/revisions"}],"predecessor-version":[{"id":1085,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/969\/revisions\/1085"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}