{"id":3325,"date":"2018-10-08T00:01:02","date_gmt":"2018-10-08T07:01:02","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3325"},"modified":"2018-10-06T12:41:43","modified_gmt":"2018-10-06T19:41:43","slug":"initial-caps-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3325","title":{"rendered":"Initial Caps &#8211; Solution"},"content":{"rendered":"<p>The solution for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3307\">this month&#8217;s Exercise<\/a> is perhaps a bit trickier than you may have anticipated. The issue is to set the first letter of a word as upper case and the remaining letters lowercase. The solution I devised involves reading two characters at a time.<br \/>\n<!--more--><br \/>\nI&#8217;m sure other solutions are possible, but for mine I created two variables: <code>ch<\/code> and <code>prev<\/code>. Both are integers, as to write a proper filter you must test for the <code>EOF<\/code> (End Of File) condition. Variable <code>ch<\/code> holds the current character and <code>prev<\/code> holds the previously-read character.<\/p>\n<p>By holding two variables, you can confirm whether a letter is at the start of a word; if both <code>prev<\/code> and <code>ch<\/code> are letters, then <code>prev<\/code> must be capitalized and <code>ch<\/code> not. The only tussle is how to read the previous character when the code first runs. Here&#8217;s my solution:<\/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    int ch,prev;\r\n\r\n    <span class=\"comments\">\/* must initialize prev *\/<\/span>\r\n    prev = '\\0';\r\n\r\n    <span class=\"comments\">\/* filter loop *\/<\/span>\r\n    while(1)\r\n    {\r\n        <span class=\"comments\">\/* fetch input *\/<\/span>\r\n        ch = getc(stdin);\r\n        <span class=\"comments\">\/* quit on EOF *\/<\/span>\r\n        if( ch == EOF)\r\n            break;\r\n        \r\n        <span class=\"comments\">\/* process input *\/<\/span>\r\n        if( isalpha(prev) )\r\n            ch = tolower(ch);\r\n        if( isspace(prev) || prev=='\\0' || prev=='(' )\r\n            ch = toupper(ch);\r\n\r\n        <span class=\"comments\">\/* send output *\/<\/span>\r\n        putc(ch,stdout);\r\n        <span class=\"comments\">\/* save current as previous character *\/<\/span>\r\n        prev = ch;\r\n    }\r\n}<\/pre>\n<p>Variable <code>prev<\/code> must be initialized before processing starts. Because no character is available at the beginning of input, <code>prev<\/code> is set to the null character at Line 9.<\/p>\n<p>In the endless <em>while<\/em> loop, input is fetched at Line 15 and the <code>EOF<\/code> character tested at Line 17.<\/p>\n<p>At Line 21, the value of character <code>prev<\/code> is tested. When <em>isalpha()<\/em> is true, <code>prev<\/code> holds a letter of the alphabet, upper or lower case. Therefore, the value of <code>ch<\/code> should be lower case, and it&#8217;s converted at Line 22.<\/p>\n<p>If the previous character is a space (tab, enter, space) or null, or a beginning parentheses, the value of <code>ch<\/code> is set to uppercase at Line 24.<\/p>\n<p>Line 27 sends character <code>ch<\/code> to standard output. And line 29 assigns the value of <code>prev<\/code> to the value of <code>ch<\/code> and the loop continues.<\/p>\n<p>I tried to conceive of other ways to process an initial caps filter, but it just can&#8217;t be done without storing at least one character of input. For example, it&#8217;s possible to read an entire line (or buffer) and process it on output. Still, the solution I devised seems simple enough.<\/p>\n<p>With C programming, no two solutions need to be the same, nor is there any one solution that&#8217;s better than the others. If you coded a solution that properly processes input and translates the words output as initial caps, great!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The solution for this month&#8217;s Exercise is perhaps a bit trickier than you may have anticipated. The issue is to set the first letter of a word as upper case and the remaining letters lowercase. The solution I devised involves &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3325\">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-3325","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\/3325","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=3325"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3325\/revisions"}],"predecessor-version":[{"id":3328,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3325\/revisions\/3328"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}