{"id":3307,"date":"2018-10-01T00:01:18","date_gmt":"2018-10-01T07:01:18","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3307"},"modified":"2018-10-10T07:26:12","modified_gmt":"2018-10-10T14:26:12","slug":"initial-caps","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3307","title":{"rendered":"Initial Caps"},"content":{"rendered":"<p>A filter is a program that accepts standard input and generates standard output. These programs are fun to write, providing you have a goal in mind and some way to manipulate the incoming text in a manner that&#8217;s significant enough to notice in the output.<br \/>\n<!--more--><br \/>\nFor the past five years on this blog, I&#8217;ve written several posts on filters:<\/p>\n<p>Lesson &#8211; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=190\">The Caesarian Cipher<\/a><br \/>\nLesson &#8211; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=711\">One Word at a Time<\/a><br \/>\nSeries &#8211; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1601\">Encoding and Decoding<\/a><br \/>\nLesson &#8211; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2804\">Safe Coding Practices <em>getchar()<\/em> and <em>putchar()<\/em><\/a><br \/>\nLesson &#8211; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=689\">A Word Wrap Filter (Part 1 of 2)<\/a><br \/>\nExercise &#8211; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2626\">The URL Encoding Filter<\/a><br \/>\nExercise &#8211; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2672\">The URL Decoding Filter<\/a><\/p>\n<p>It&#8217;s time for another!<\/p>\n<p>This idea came to me while I was working on another programming project. I needed to generate text in Title Case, which is also known as Initial Caps. The goal is to capitalize the first letter of each word. I figured it&#8217;s a good task for a filter.<\/p>\n<p>For a filter, all input comes from standard input, or the <em>stdin<\/em>, device. All output is sent to standard output, the <em>stdout<\/em> device. Both <em>stdin<\/em> and <em>stdout<\/em> are defined in C, which means you can use standard I\/O or file I\/O functions to fetch and put characters.<\/p>\n<p>The following code presents a simple I\/O filter that doesn&#8217;t modify input:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\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        putc(ch,stdout);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop between Lines 7 and 13 is an infinite loop, which is typical for a filter. The <em>getc()<\/em> function at Line 9 fetches a character from standard input, <code>stdin<\/code>.<\/p>\n<p>At Line 10, the <code>EOF<\/code> (End of File) condition is checked for. If found, the loop is broken and the program stops. <code>EOF<\/code> is an integer constant, which is why variable <code>ch<\/code> is defined as an <em>int<\/em>, not a <em>char<\/em>.<\/p>\n<p>Line 12 outputs the same character. In a true filter, like the one you are challenged to create for this Exercise, more action must happen between the <em>if<\/em> test and <em>putchar()<\/em>.<\/p>\n<p>Using the skeleton provided above (or you can create your own), process input so that the first letter of a word is capitalized and the rest of the characters in the word appear as lower case.<\/p>\n<p>Here are some sample runs to demonstrate what the filter does:<\/p>\n<p><code>this is all lower case<br \/>\nThis Is All Lower Case<br \/>\nTHIS IS ALL CAPS<br \/>\nThis Is All Caps<br \/>\nThis Is Already Initial Caps<br \/>\nThis Is Already Initial Caps<\/code><\/p>\n<p>The odd-numbered lines (1, 3, and 5) appear as typed. After Enter is pressed, the filter spits out the modified line (lines 2, 4, and 6).<\/p>\n<p>Your task is to write such a filter. It might be trickier than you think, but it can be done! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3325\">Click here<\/a> to view  my solution, though please try this Exercise on your own before you see how I&#8217;ve solved it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Gently massage text so that Each Word Is Capitalized. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3307\">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-3307","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\/3307","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=3307"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3307\/revisions"}],"predecessor-version":[{"id":3335,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3307\/revisions\/3335"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}