{"id":3869,"date":"2019-12-01T00:01:04","date_gmt":"2019-12-01T08:01:04","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3869"},"modified":"2019-12-08T19:29:10","modified_gmt":"2019-12-09T03:29:10","slug":"morse-code-filter","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3869","title":{"rendered":"Morse Code Filter"},"content":{"rendered":"<p>I&#8217;m certain that the nerds would love debating whether the telegraph&#8217;s Morse code system was the first binary communications network. Let them do so. Still, Morse code remains a simple communications system, translating letters and numbers into dots and dashes &mdash; which you could argue are similar to ones and zeros.<br \/>\n<!--more--><br \/>\nSurprisingly, Morse code (obligatory <a href=\"https:\/\/en.wikipedia.org\/wiki\/Morse_code\" rel=\"noopener noreferrer\" target=\"_blank\">Wikipedia link<\/a>) has no official value for space or any punctuation characters. Its codes represent alphabetic letters A to Z, digits 0 through 9. A pause, or gap, in transmission separates letters, words, and so on, which is where the nerds could argue makes the system ternary and not binary.<\/p>\n<p>Regardless, your task for this month&#8217;s Exercise is to concoct a filter that translates standard input into Morse code characters, period (.) for dot and hyphen (-) for dash. Opinions differ on how to represent the translation when output as text, so my advice for your code is to output words on a line by themselves. Separate the character codes with spaces (see below). Don&#8217;t output anything for punctuation or other undefined characters.<\/p>\n<p>Here&#8217;s a sample run of my code:<\/p>\n<p><code>Goodbye, cruel world!<br \/>\n--. --- --- -.. -... -.-- .<br \/>\n-.-. .-. ..- . .-..<br \/>\n.-- --- .-. .-.. -.. <\/code><\/p>\n<p>See how each word appears on a line by itself, with the codes (dots and dashes) separated by spaces?<\/p>\n<p>To help get you started, here is a code skeleton you can build upon:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.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\r\n    <span class=\"comments\">\/* your solution goes here *\/<\/span>\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>Your task is to code the full <em>toMorse()<\/em> function.<\/p>\n<p>Please try this Exercise on your own before you peek at <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3884\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Translate standard input into Morse code. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3869\">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":[3],"tags":[],"class_list":["post-3869","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\/3869","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=3869"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3869\/revisions"}],"predecessor-version":[{"id":3905,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3869\/revisions\/3905"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}