{"id":5476,"date":"2022-08-08T00:01:19","date_gmt":"2022-08-08T07:01:19","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5476"},"modified":"2022-08-06T11:11:25","modified_gmt":"2022-08-06T18:11:25","slug":"from-a-and-a-to-z-and-z-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5476","title":{"rendered":"From &#8216;A&#8217; and &#8216;a&#8217; to &#8216;Z&#8217; and &#8216;z&#8217; &#8211; Solution"},"content":{"rendered":"<p>I don&#8217;t believe one definitive solution exists for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5469\">this month&#8217;s Exercise<\/a>: Output a string of upper- and lowercase letters. No, the challenge is more to discover different insights a coder has to a specific problem.<br \/>\n<!--more--><br \/>\nHere is the desired output string:<\/p>\n<p><code>AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz<\/code><\/p>\n<p>My first approach is one I consider the most obvious:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_08-Exercise-a.c\" rel=\"noopener\" target=\"_blank\">2022_08-Exercise-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for( x='A'; x&lt;='Z'; x++ )\r\n        printf(\"%c%c\",x,x+32);\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>A <em>for<\/em> loop plows through characters <code>'A'<\/code> through <code>'Z'<\/code> at Line 7. The character is output at Line 8, along with its companion lowercase letter, which is <code>x+32<\/code> (thank you ASCII table). This solution was my first stab, though it could also be done by using two variables instead:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_08-Exercise-b.c\" rel=\"noopener\" target=\"_blank\">2022_08-Exercise-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x,y;\r\n\r\n    for( x='A',y='a'; x&lt;='Z'; x++,y++ )\r\n        printf(\"%c%c\",x,y);\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here both <code>x<\/code> and <code>y<\/code> are initialized in the <em>for<\/em> loop. Only one terminating condition needs to be specified, <code>x<='Z'<\/code>. Then both <code>x<\/code> and <code>y<\/code> are incremented each turn. This solution avoids math in the <em>printf()<\/em> statement, but it requires a knowledge that you can perform multiple actions within a single <em>for<\/em> loop statement.<\/p>\n<p>This next solution uses a <em>while<\/em> loop and a single <em>char<\/em> variable <code>alphabet<\/code>:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_08-Exercise-c.c\" rel=\"noopener\" target=\"_blank\">2022_08-Exercise-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char alphabet = 'A';\r\n\r\n    while( alphabet!='Z'+1)\r\n    {\r\n        putchar(alphabet);\r\n        putchar(alphabet+32);\r\n        alphabet++;\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop spins as long as the value of <code>alphabet<\/code> doesn't equal <code>'Z'+1<\/code>. This expression allows letters Z and z to be output, because the loop stops after Z is output. The <em>putchar()<\/em> at Line 10 adds 32 to the value of <code>alphabet<\/code>, generating lowercase letter output. Most importantly, <code>alphabet<\/code> is incremented at Line 11, which keeps the loop moving.<\/p>\n<p>Now I get bizarre:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_08-Exercise-d.c\" rel=\"noopener\" target=\"_blank\">2022_08-Exercise-d.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    <span class=\"comments\">\/* size the array for two alphabets (26)\r\n       and the null character *\/<\/span>\r\n    char a[26*2+1];\r\n    char alpha;\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* do uppercase *\/<\/span>\r\n    alpha = 'A';\r\n    for( x=0; x&lt;26*2; x+=2 )\r\n    {\r\n        a[x] = alpha;\r\n        alpha++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* do lowercase *\/<\/span>\r\n    alpha = 'a';\r\n    for( x=0; x&lt;26*2; x+=2 )\r\n    {\r\n        a[x+1] = alpha;\r\n        alpha++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* append the null char *\/<\/span>\r\n    a[26+26] = '\\0';\r\n\r\n    <span class=\"comments\">\/* output result *\/<\/span>\r\n    puts(a);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Two <em>for<\/em> loops fill array <code>a[]<\/code>. The first sets uppercase letters to every other position in the array. The second <em>for<\/em> loop works similarly, but for lowercase and using <code>a[x+1]<\/code> as its index value. The <code>+1<\/code> shifts the lowercase letters over, squeezing them between uppercase letters already set in the array. It's kinda crazy, but it doesn't attain the level of insanity as this version:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_08-Exercise-e.c\" rel=\"noopener\" target=\"_blank\">2022_08-Exercise-e.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    const int size = 26*2+1;\r\n    char *a;\r\n    int x,y;\r\n    \r\n    <span class=\"comments\">\/* allocate storage for two alphabets plus\r\n       a null character *\/<\/span>\r\n    a = malloc( sizeof(char) * size );\r\n    if( a==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to allocate memory\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* initialize the buffer to all As *\/<\/span>\r\n    for( x=0; x&lt;size; x++ )\r\n        *(a+x) = 'A';\r\n\r\n\r\n    <span class=\"comments\">\/* manipulate contents *\/<\/span>\r\n    for( x=0,y=0; x&lt;26*2; x+=2,y++ )\r\n    {\r\n        *(a+x) = *(a+x) + y;\r\n        *(a+x+1) = *(a+x+1) + y + 32;\r\n    }\r\n\r\n    <span class=\"comments\">\/* output the results *\/<\/span>\r\n    *(a+size-1) = '\\0';\r\n    puts(a);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>My nuttiest solution uses a pointer, allocates storage, then fills the buffer with uppercase letter <code>'A'<\/code>. The action takes place at Line 25, where a <em>for<\/em> loop uses two variables to process the buffer. The first looping variable <code>x<\/code> is increased by two each turn; variable <code>y<\/code> incremented by one.<\/p>\n<p>The statements at Lines 27 and 28 adjust the value of character <code>'A'<\/code> (already in the buffer) to create the mixed upper- lowercase string. It took a few tries for me to code something so convoluted, but it works.<\/p>\n<p>I hope you came up with a working solution, easy or hard. If you're an experienced coder, I hope you wove a tapestry of statements that inspires and delights. That's the point of the exercise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t believe one definitive solution exists for this month&#8217;s Exercise: Output a string of upper- and lowercase letters. No, the challenge is more to discover different insights a coder has to a specific problem.<\/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-5476","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\/5476","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=5476"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5476\/revisions"}],"predecessor-version":[{"id":5483,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5476\/revisions\/5483"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}