{"id":1031,"date":"2014-11-08T00:01:40","date_gmt":"2014-11-08T08:01:40","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1031"},"modified":"2014-11-01T08:17:51","modified_gmt":"2014-11-01T15:17:51","slug":"from-text-to-integer-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1031","title":{"rendered":"From Text to Integer &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s Exercise is to code a function that converts a string of text numbers into an integer value. If you used the skeleton provided, then your program&#8217;s output would look like this:<\/p>\n<pre><code>The string 24680 represents value 24680.<\/code><\/pre>\n<p><!--more--><br \/>\n<a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/11\/11exercise.c\">Click here<\/a> to view\/download my solution.<\/p>\n<p>Here&#8217;s what I did in the <em>convertString()<\/em> function, which starts at Line 18 in my example:<\/p>\n<pre class=\"screen\">\r\nint convertString(char *string)\r\n{\r\n    char *s;\r\n    int total,byten;<\/pre>\n<p>I added a second pointer, <code>s<\/code>, because I don&#8217;t want to lose track of the base address held in <code>string<\/code>. The <code>total<\/code> variable holds the calculated integer total, what programmers would call an <em>accumulator<\/em>. <code>byten<\/code> is the power-of-tens operator, which I&#8217;ll explain in a bit.<\/p>\n<p>The next chunk of code (at Line 23) initializes the variables:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* initialize variables *\/<\/span>\r\n    s = string;\r\n    total = 0;\r\n    byten = 1;<\/pre>\n<p>Numbers are read left-to-right, but by going right-to-left you get the smaller values first. So to convert the string, the code starts the conversion with the smallest value. The next part of the code locates that terminus:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* find the last character in the string *\/<\/span>\r\n    while(*s)\r\n    {\r\n        s++;\r\n    }\r\n    s--;<\/pre>\n<p>Pointer <code>s<\/code> hunts down the last character in the string. That character is the <code>'\\0'<\/code> null character, which reads as FALSE and thus terminates the <em>while<\/em> loop.<\/p>\n<p>The <code>s--<\/code> operation (Line 33) backs up the pointer to reference that character before the <code>'\\0'<\/code>, which is the one&#8217;s digit in the string.<\/p>\n<blockquote><p>If you were writing a text-to-<em>int<\/em> function, then you&#8217;d want to do more character testing as you backup through the string. Ensure that each character is a digit and, if not, terminate the loop at the first non-digit.<\/p><\/blockquote>\n<p>The workhorse loop in the function starts at Line 36:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* Do the math *\/<\/span>\r\n    while(s &gt;= string)\r\n    {\r\n        total += (*s - '0') * byten;\r\n        byten *= 10;\r\n        s--;\r\n    }<\/pre>\n<p>The <em>while<\/em> loop backs up through the digits, which is why the condition is <code>s &gt;= string<\/code>. The <code>s<\/code> pointer is decremented (in the code at Line 40), which is how the loop moves.<\/p>\n<p>The <code>total<\/code> calculation (Line 38) takes the ASCII value of character <code>*s<\/code> and multiplies it by the value of the <code>byten<\/code> variable. For the first (far right) character in the number, the <code>byten<\/code> variable is 1 &#8212; the one&#8217;s position.<\/p>\n<p>The value of <code>byten<\/code> is then multiplied by 10 for the 10&#8217;s place, then 100&#8217;s, 1000&#8217;s and so on.<\/p>\n<p>As this loop repeats, the values are accumulated in the <code>total<\/code> variable. When the <em>while<\/em> loop is done, meaning that no more characters are left to read, the value <code>total<\/code> is returned from the function.<\/p>\n<p>Yes! This was a tough assignment! Kudos if you managed it on your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise is to code a function that converts a string of text numbers into an integer value. If you used the skeleton provided, then your program&#8217;s output would look like this: The string 24680 represents value 24680.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1031","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\/1031","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=1031"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1031\/revisions"}],"predecessor-version":[{"id":1075,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1031\/revisions\/1075"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}