{"id":6704,"date":"2024-12-14T00:01:09","date_gmt":"2024-12-14T08:01:09","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6704"},"modified":"2024-12-21T08:32:56","modified_gmt":"2024-12-21T16:32:56","slug":"the-pig-latin-translator-part-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6704","title":{"rendered":"The Pig Latin Translator, Part I"},"content":{"rendered":"<p>One of my older C programming books featured a sample program that translated English words into their Pig Latin equivalent. It&#8217;s time to revisit this code.<br \/>\n<!--more--><br \/>\nThe program I wrote back in 2004, published in the <em>C All-in-One Desktop Reference For Dummies<\/em> (Wiley)(over 800 pages long!), is a filter. It&#8217;s a good, fun example for working a filter, but the code is long and I believe I could write it better 20 years later.<\/p>\n<p>First, what is Pig Latin?<\/p>\n<p>It&#8217;s English, but with the first part of the word flopped to the end. Either <em>-ay<\/em> or <em>-hay<\/em> is added, which makes it sound like Ecclesiastical Latin. (Think of a Roman Catholic priest speaking in an echoey church.) So, <em>Hello<\/em> becomes <em>ello-Hay<\/em>.<\/p>\n<p>Second, the language has no rules. It&#8217;s a play on words &mdash; and an old one. Shakespeare&#8217;s play <em>Love&#8217;s Labour&#8217;s Lost<\/em> includes this snippet, called Dog Latin in the day: <em>O, I smell false Latine; dunghill for unguem.<\/em> Dog Latin evolved into Hog Latin and then to Pig Latin. I suppose the only &#8220;rule&#8221; is that it sounds like Latin. (Though it doesn&#8217;t, really.)<\/p>\n<p>Third, to code it I&#8217;m writing a <em>piglatin()<\/em> function. The first step is to swallow the word and send it off to the function. I wrote the following skeleton to fetch a text from standard input, lop off the newline, then send the word (the text) to the <em>piglatin()<\/em> function and return it unmodified:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_12_14-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_12_14-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n<span class=\"comments\">\/* translate a word into piglatin *\/<\/span>\r\nchar *piglatin(char *s)\r\n{\r\n    <span class=\"comments\">\/* word starts with a vowel, add -hay *\/<\/span>\r\n    <span class=\"comments\">\/* move first consonant\/consonant cluster\r\n       add -ay *\/<\/span>\r\n    return s;\r\n}\r\n\r\nint main()\r\n{\r\n    char word[32];\r\n    char *s,*pl;\r\n\r\n    printf(\"Original word: \");\r\n    fgets(word,32,stdin);\r\n    <span class=\"comments\">\/* remove newline *\/<\/span>\r\n    s = word;\r\n    while(*s++)\r\n        if( *s=='\\n') *s='\\0';\r\n\r\n    <span class=\"comments\">\/* translate to pig latin *\/<\/span>\r\n    pl = piglatin(word);\r\n\r\n    printf(\"Translated word: %s\\n\",pl);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The bulk of the <em>main()<\/em> function removes the newline automatically added by the <em>fgets()<\/em> function. The input text is then sent to the <em>piglatin()<\/em> function, which returns the word for output. Here&#8217;s a sample run:<\/p>\n<p><code>Original word: hello<br \/>\nTranslated word: hello<\/code><\/p>\n<p>The code contains a comment in the <em>piglatin()<\/em> function that describes the pending operation: When the word starts with a vowel, affix the <code>-hay<\/code> ending. Otherwise, move the first consonant or consonant cluster to the end of the word and add <code>-ay<\/code>. Variations on these techniques differ. For example, some Pig Latin translations use <em>yay<\/em> instead of <em>hay<\/em>. Some omit the hyphens.<\/p>\n<p>Rather than tackle the entire problem at once, my next update works only on those words that start with a vowel. To accomplish this task, I added a <em>switch-case<\/em> structure to the <em>piglatin()<\/em> function. For those words starting with a vowel, a new string is allocated with storage for the <code>-hay<\/code> suffix. Then the new, Pig Latin word is built:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_12_14-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2024_12_14-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n<span class=\"comments\">\/* translate a word into piglatin *\/<\/span>\r\nchar *piglatin(char *s)\r\n{\r\n    char *pig;\r\n\r\n    <span class=\"comments\">\/* word starts with a vowel, add -hay *\/<\/span>\r\n    switch(*s)\r\n    {\r\n        case 'a':\r\n        case 'e':\r\n        case 'i':\r\n        case 'o':\r\n        case 'u':\r\n            pig = malloc( strlen(s)+4+1 );    <span class=\"comments\">\/* \"-hay\" + '\\0' *\/<\/span>\r\n            if( pig==NULL )\r\n                return(\"Memory Error\");\r\n            strcpy(pig,s);\r\n            strcat(pig,\"-hay\");\r\n            break;\r\n    <span class=\"comments\">\/* move first consonant\/consonant cluster\r\n       add -ay *\/<\/span>\r\n        default:\r\n            pig = s;    <span class=\"comments\">\/* temp assignment *\/<\/span>\r\n    }\r\n    return pig;\r\n}\r\n\r\nint main()\r\n{\r\n    char word[32];\r\n    char *s,*pl;\r\n\r\n    printf(\"Original word: \");\r\n    fgets(word,32,stdin);\r\n    <span class=\"comments\">\/* remove newline *\/<\/span>\r\n    s = word;\r\n    while(*s++)\r\n        if( *s=='\\n') *s='\\0';\r\n\r\n    <span class=\"comments\">\/* translate to pig latin *\/<\/span>\r\n    pl = piglatin(word);\r\n\r\n    printf(\"Translated word: %s\\n\",pl);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>In the <em>piglatin()<\/em> function, when the word starts with a vowel, pointer <code>*pig<\/code> is allocated enough storage for the original word plus 4 for <code>-hay<\/code> and one for the null character. The original string is copied to <code>pig<\/code>, then <code>-hay<\/code> is concatenated. The word is returned. For all other words, the original word is returned.<\/p>\n<p>Here are some sample runs:<\/p>\n<p><code>Original word: outside<br \/>\nTranslated word: outside-hay<\/p>\n<p>Original word: nothing<br \/>\nTranslated word: nothing<\/code><\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6718\">next week&#8217;s Lesson<\/a>, I continue to build the Pig Latin translator by improving upon this code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>anslateTr-ay ordsw-ay into-hay igP-ay atinL-ay <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6704\">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":[2],"tags":[],"class_list":["post-6704","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6704","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=6704"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6704\/revisions"}],"predecessor-version":[{"id":6773,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6704\/revisions\/6773"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}