{"id":3884,"date":"2019-12-08T00:01:46","date_gmt":"2019-12-08T08:01:46","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3884"},"modified":"2019-12-07T09:14:46","modified_gmt":"2019-12-07T17:14:46","slug":"morse-code-filter-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3884","title":{"rendered":"Morse Code Filter &#8211; Solution"},"content":{"rendered":"<p>A Morse Code filter probably has no practical use, but it&#8217;s a good programming exercise. The issue is how to deal with undefined character codes and otherwise present the output. My solution for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3869\">this month&#8217;s Exercise<\/a> involves interpreting standard input, discarding undefined information, and sending the results to standard output in a format that isn&#8217;t ugly.<br \/>\n<!--more--><br \/>\nA filter processes input one character at a time. The character must be defined as an integer, which is necessary to catch the <em>EOF<\/em> should input be redirected from a file. This task is handled in the <em>main()<\/em> function, provided in the skeleton offered:<\/p>\n<p><code>while(1)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;ch = getc(stdin);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if( ch==EOF )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;toMorse(ch);<br \/>\n}<\/code><\/p>\n<p>Within the <em>toMorse()<\/em> function, characters are manipulated by the <code>ctype.h<\/code> header functions as well as a standard <em>if<\/em> test:<\/p>\n<ol>\n<li>Use <em>isalpha()<\/em> to test for alphabetic characters and process.<\/li>\n<li>Use <em>isdigit()<\/em> to test for numbers 0 through 9 and process.<\/li>\n<li>Use an <em>if<\/em> condition to test for spaces and newlines.<\/li>\n<\/ol>\n<p>For the <em>isalpha()<\/em> part of the code, the character is converted to uppercase as Morse Code is case-insensitive. The value <code>'A'<\/code> is subtracted from the result, which is used as an index into the <code>morse_alpha[]<\/code> array. The matching string is output.<\/p>\n<p>The <em>isdigit()<\/em> part of the code works the same as <em>isalpha()<\/em>, though the character need not be converted to uppercase and the index is used in the <code>morse_digit[]<\/code> array.<\/p>\n<p>When the space or newline are encountered (the final <em>if<\/em> test), a newline is output.<\/p>\n<p>For all other characters, the code returns without generating any output.<\/p>\n<p>Here is the full code for my solution, which is also <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_12-Exercise.c\" rel=\"noopener noreferrer\" target=\"_blank\">published on GitHub<\/a>:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\nvoid toMorse(char c)\r\n{\r\n    char *morse_alpha[] = {\r\n        \".-\", \"-...\", \"-.-.\", \"-..\", \".\", \"..-.\",\r\n        \"--.\", \"....\", \"..\", \".---\", \"-.-\", \".-..\",\r\n        \"--\", \"-.\", \"---\", \".--.\", \"--.-\", \".-.\",\r\n        \"...\", \"-\", \"..-\", \"...-\", \".--\", \"-..-\",\r\n        \"-.--\", \"--..\"\r\n    };\r\n    char *morse_digit[] = {\r\n        \"-----\", \".----\", \"..---\", \"...--\", \"....-\",\r\n        \".....\", \"-....\", \"--...\", \"---..\", \"----.\"\r\n    };\r\n    int m;\r\n\r\n    if( isalpha(c) )\r\n    {\r\n        <span class=\"comments\">\/* Morse is case-insensitive *\/<\/span>\r\n        c = toupper(c);\r\n        m = c - 'A';\r\n        printf(\"%s \",morse_alpha[m]);\r\n    }\r\n    else if( isdigit(c) )\r\n    {\r\n        m = c - '0';\r\n        printf(\"%s \",morse_digit[m]);\r\n    }\r\n    else if( c==' ' || c=='\\n' )\r\n    {\r\n        putchar('\\n');\r\n    }\r\n    else\r\n        return;\r\n}\r\n\r\nint main()\r\n{\r\n    int ch;\r\n\r\n    while(1)\r\n    {\r\n        ch = getc(stdin);\r\n        if( ch==EOF )\r\n            break;\r\n        toMorse(ch);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>ctype.h<\/code> header file included in my solution isn&#8217;t included in the original skeleton. It&#8217;s possible to concoct a solution without using any ctype functions, but I&#8217;m lazy. Further, much of the code could be condensed, though I favor readable code over brief code.<\/p>\n<p>If your solution results in Morse Code output based on all forms of input, filtering out any undefined characters, consider your solution worthy. Even if you think this challenge is too easy, it&#8217;s always good to hone your programming skills. You never know when a solution to a simple problem can offer insight into a larger more complex puzzle. Such is the nature of computer programming.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Morse Code filter probably has no practical use, but it&#8217;s a good programming exercise. The issue is how to deal with undefined character codes and otherwise present the output. My solution for this month&#8217;s Exercise involves interpreting standard input, &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3884\">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-3884","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\/3884","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=3884"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3884\/revisions"}],"predecessor-version":[{"id":3894,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3884\/revisions\/3894"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}