{"id":7091,"date":"2025-08-08T00:01:44","date_gmt":"2025-08-08T07:01:44","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7091"},"modified":"2025-08-02T10:06:26","modified_gmt":"2025-08-02T17:06:26","slug":"your-name-in-base-36-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7091","title":{"rendered":"Your Name in Base 36 &#8211; Solution"},"content":{"rendered":"<p>My solution for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7085\">this month&#8217;s Exercise<\/a> didn&#8217;t require much work. What I did was to &#8220;stack overflow&#8221; the problem by pulling in functions from earlier Lessons. The only new portion of code deals with processing the input before sending the string off to the <em>b36_decimal()<\/em> function I&#8217;ve already written.<br \/>\n<!--more--><br \/>\nOh, and the <em>b36_decimal()<\/em> function also needs the <em>base36_powers()<\/em> function, if you opt to code the solution as I did:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_08-Exercise.c\" rel=\"noopener\" target=\"_blank\">2025_08-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\n#define BASE36_MAX 36\r\n#define SIZE 10\r\n\r\n<span class=\"comments\">\/* build the base 36 powers table *\/<\/span>\r\nlong *base36_powers(void)\r\n{\r\n    static long b36_powers[SIZE];\r\n    int x;\r\n\r\n    for( x=0; x&lt;SIZE; x++ )\r\n        b36_powers[x] = x ? b36_powers[x-1]*BASE36_MAX : 1;\r\n\r\n    return(b36_powers);\r\n}\r\n\r\n<span class=\"comments\">\/* return decimal value for base 36 string v *\/<\/span>\r\nlong b36_decimal(char *v)\r\n{\r\n    long *powers,result;\r\n    int x,y,digit;\r\n\r\n    <span class=\"comments\">\/* obtain the powers table *\/<\/span>\r\n    powers = base36_powers();\r\n\r\n    <span class=\"comments\">\/* must process the string backwards *\/<\/span>\r\n    x = 0;\r\n    while( *(v+x) != '\\0' )        <span class=\"comments\">\/* locate the string's end *\/<\/span>\r\n        x++;\r\n    x--;                        <span class=\"comments\">\/* backup to 1's position *\/<\/span>\r\n\r\n    <span class=\"comments\">\/* process the digits (x) and powers (y) *\/<\/span>\r\n    result = 0;\r\n    y = 0;\r\n    while(x&gt;=0)\r\n    {\r\n        <span class=\"comments\">\/* translate digit *\/<\/span>\r\n        if( *(v+x)&gt;='0' &amp;&amp; *(v+x)&lt;='9' )\r\n            digit = *(v+x) - '0';\r\n        else\r\n            digit = *(v+x) - 'A' + 10;\r\n        <span class=\"comments\">\/* fetch power value *\/<\/span>\r\n        result += digit * *(powers+y);\r\n        x--;\r\n        y++;\r\n    }\r\n\r\n    return(result);\r\n}\r\n\r\nint main()\r\n{\r\n    char name[32];\r\n    long r;\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* prompt for input *\/<\/span>\r\n    printf(\"Enter your name: \");\r\n    fgets(name,32,stdin);\r\n    <span class=\"comments\">\/* remove newline and set to uppercase *\/<\/span>\r\n    x = 0;\r\n    while(x&lt;32)\r\n    {\r\n        if( name[x]=='\\n' )\r\n        {\r\n            name[x] = '\\0';\r\n            break;\r\n        }\r\n        name[x] = toupper(name[x]);\r\n        x++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* translate the b36 name into decimal *\/<\/span>\r\n    r = b36_decimal(name);\r\n    printf(\"Base 36 %s in decimal is %ld\\n\",name,r);\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    return 0;\r\n}<\/pre>\n<p>The user is prompted for a name in the <em>main()<\/em> function. I use the <em>fgets()<\/em> function for input, which is safe. The <em>while<\/em> loop pulls the newline from the end of input as well as converts letters to uppercase. I don&#8217;t perform any further checks, so the user could input anything. Nothing serious happens when invalid input flies in.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Enter your name: danny<br \/>\nBase 36 DANNY in decimal is 22332238<\/code><\/p>\n<p>Yes, this Exercise isn&#8217;t much of a challenge, because it&#8217;s merely cobbling together existing code. Still, that&#8217;s what a lot of coders do and I think doing so is okay &mdash; if it&#8217;s your own code! For heaven&#8217;s sake, don&#8217;t let AI do the job if you ever expect to learn anything or to become a valuable resource as a programmer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My solution for this month&#8217;s Exercise didn&#8217;t require much work. What I did was to &#8220;stack overflow&#8221; the problem by pulling in functions from earlier Lessons. The only new portion of code deals with processing the input before sending the &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7091\">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":[5],"tags":[],"class_list":["post-7091","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\/7091","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=7091"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7091\/revisions"}],"predecessor-version":[{"id":7108,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7091\/revisions\/7108"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}