{"id":1438,"date":"2015-07-08T00:01:47","date_gmt":"2015-07-08T07:01:47","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1438"},"modified":"2015-07-04T08:53:58","modified_gmt":"2015-07-04T15:53:58","slug":"spelling-alphabet-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1438","title":{"rendered":"Spelling Alphabet &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s task was to create code that translates spelling from a single word into the corresponding NATO phonetic alphabet words. For input such as <em>hello<\/em> the output would be something like:<\/p>\n<p><code>Hotel Echo Lima Lima Oscar<\/code><br \/>\n<!--more--><br \/>\nFor my solution, I used an array of 26 pointers to represent the phonetic alphabet words. The word input is dissected one letter at a time. That letter&#8217;s offset (from A) is used to reference each phonetic alphabet word in the array. So the letter &#8216;B&#8217; references element 1 in the array, &#8220;Bravo.&#8221; (Remember, arrays start numbering at element zero.)<\/p>\n<p>Here&#8217;s the code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\nint main()\r\n{\r\n    char *npa[26] = {\r\n        \"Alfa\", \"Bravo\", \"Charlie\", \"Delta\", \"Echo\",\r\n        \"Foxtrot\", \"Golf\", \"Hotel\", \"India\", \"Juliett\",\r\n        \"Kilo\", \"Lima\", \"Mike\", \"November\", \"Oscar\",\r\n        \"Papa\", \"Quebec\", \"Romeo\", \"Sierra\", \"Tango\",\r\n        \"Uniform\", \"Victor\", \"Whiskey\", \"Xray\",\r\n        \"Yankee\", \"Zulu\" };\r\n    char word[24];\r\n    int i = 0;\r\n    char c;\r\n\r\n    printf(\"Type a word: \");\r\n    scanf(\"%23s\",word);\r\n\r\n    while(word[i])\r\n    {\r\n        c = toupper(word[i]);\r\n        if( !isalpha(c) )\r\n            break;\r\n        printf(\"%s \",npa[c-'A']);\r\n        i++;\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>word<\/code> buffer holds input text. I set its size value to 24, which is a goodly length, but not too long.<\/p>\n<p>The <em>scanf()<\/em> function at Line 18 is ideal to read in a limited amount of text. That&#8217;s because <em>scanf()<\/em> stops reading at the first whitespace character. For gobbling a word, it&#8217;s perfect.<\/p>\n<p>I also took advantage of the <em>scanf()<\/em> function&#8217;s <code>%s<\/code> placeholder to cap input. In this instance, only 23 characters are read by setting the <code>%s<\/code> placeholder to a width of 24. That allows for the null character (<code>\\0<\/code>) at the end of the string. The potential for overflow is checked.<\/p>\n<p>The next step in the code is to loop through input one character at a time. I chose a <em>while<\/em> loop at Line 20 using the current letter in the <code>word<\/code> buffer as an index. The letter is converted to uppercase by the <em>toupper()<\/em> function at Line 22. The <em>printf()<\/em> statement at Line 25 obtains the offset by subtracting the upper case letter&#8217;s ASCII value from the letter &#8216;A&#8217;. The value generated is used as the <code>npa<\/code> array index.<\/p>\n<p>The <em>islpha()<\/em> test at Line 24 confirms that the word contains only letters of the alphabet. The <em>scanf()<\/em> function stops reading at whitespace, but it does include symbols and numbers as part of a word. The <em>if<\/em> test at Line 24 pops out of the loop once any non-alphabet character is encountered, otherwise the array index would be invalid and the program could crash with a memory error.<\/p>\n<p>Your solution can be different from mine, of course. As long as the phonetic alphabet is output, and you do a bit of defensive programming to ensure that nothing has the potential of screwing up, I&#8217;d consider your efforts a success.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s task was to create code that translates spelling from a single word into the corresponding NATO phonetic alphabet words. For input such as hello the output would be something like: Hotel Echo Lima Lima Oscar<\/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-1438","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\/1438","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=1438"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1438\/revisions"}],"predecessor-version":[{"id":1452,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1438\/revisions\/1452"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}