{"id":711,"date":"2014-06-01T00:01:12","date_gmt":"2014-06-01T07:01:12","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=711"},"modified":"2014-05-24T08:07:00","modified_gmt":"2014-05-24T15:07:00","slug":"one-word-at-a-time","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=711","title":{"rendered":"One Word at a Time"},"content":{"rendered":"<p>A filter is a command line tool that takes input, modifies it, and generates output. This month&#8217;s Exercise is to create such a filter, one that takes text input and spews it out one word at a time.<br \/>\n<!--more--><br \/>\nDon&#8217;t fret! I&#8217;m not asking you to create a word wrap filter, which was mulled over during most of May&#8217;s Lessons. Instead, this month&#8217;s Exercise is to create a filter that reads standard input up until a white space character is encountered. At that point, the word is displayed on a line by itself.<\/p>\n<p>For example, for the input <code>We the People of the United States<\/code> the code would generate the following output:<\/p>\n<pre><code>We\r\nthe\r\nPeople\r\nof\r\nthe\r\nUnited\r\nStates<\/code><\/pre>\n<p>Because I&#8217;m feeling generous, my simple solution to this problem is presented below.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\n#define SIZE 80\r\n\r\nint main()\r\n{\r\n    char buffer[SIZE];\r\n    int index,c;\r\n\r\n    <span class=\"comments\">\/* initalize variables *\/<\/span>\r\n    c = 1;                          <span class=\"comments\">\/* avoid random EOF *\/<\/span>\r\n    index = 0;\r\n    buffer[index] = '\\0';\r\n\r\n    <span class=\"comments\">\/* process standard input *\/<\/span>\r\n    while(c != EOF)\r\n    {\r\n        c = getchar();\r\n        if( isspace(c) )            <span class=\"comments\">\/* whitespace found *\/<\/span>\r\n        {\r\n            buffer[index] = '\\0';   <span class=\"comments\">\/* cap the string *\/<\/span>\r\n            puts(buffer);           <span class=\"comments\">\/* display w\/newline *\/<\/span>\r\n            index = 0;              <span class=\"comments\">\/* reset index *\/<\/span>\r\n        }\r\n        else                        <span class=\"comments\">\/* keep filling the buffer *\/<\/span>\r\n        {\r\n            buffer[index] = c;\r\n            index++;\r\n        }\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Don&#8217;t think you&#8217;re getting off easy this month, however. That&#8217;s because my simple solution overlooks two distinct problems. If you can already see the problems, then <em>good for you<\/em>!<\/p>\n<p>The first problem is an issue I call <em>bounds checking<\/em>, although the specific term applied to the sample code would be <em>buffer overflow<\/em>. Therefore your first task is to re-write the code so that the <code>buffer[]<\/code> doesn&#8217;t keep filling up with characters after its <code>SIZE<\/code> is encountered.<\/p>\n<p>And, no, the solution is not just to make the <code>SIZE<\/code> constant larger.<\/p>\n<p>The second problem involves a situation where perhaps more than one white space is encountered in a row. For example, the input <code>This is&nbsp;&nbsp;&nbsp;an&nbsp;&nbsp;oddly&nbsp;&nbsp;&nbsp;&nbsp;formatted&nbsp;&nbsp;sentence<\/code> still needs to be output as<\/p>\n<pre><code>This\r\nis\r\nan\r\noddly\r\nformatted\r\nsentence<\/code><\/pre>\n<p>And not:<\/p>\n<pre><code>This\r\nis\r\n\r\nan\r\n\r\n\r\noddly\r\n\r\nformatted\r\n\r\nsentence<\/code><\/pre>\n<p>Fix with my prototype code (above), or craft new code, so that these two situations are avoided.<\/p>\n<p>Please eschew your desire to peek ahead at my solution before you attempt to code your own. Further, if you&#8217;re feeling up to it, consider coding a solution by using pointers. Heck, toss in a <em>malloc()<\/em> function in there. Give yourself a challenge!<\/p>\n<p>Remember: My solutions demonstrate only one way to solve the problem. In programming, many approaches are possible, some can be more elegant or cryptic, but no one correct answer exists.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/06exercise-solution1.c\">Exercise Solution<\/a><\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/06exercise-solution2.c\">Exercise Solution with Pointers<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>See whether you can code an I\/O filter that chops text up into single words. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=711\">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-711","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\/711","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=711"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/711\/revisions"}],"predecessor-version":[{"id":744,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/711\/revisions\/744"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}