{"id":5819,"date":"2023-04-08T00:01:33","date_gmt":"2023-04-08T07:01:33","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5819"},"modified":"2023-03-25T11:24:13","modified_gmt":"2023-03-25T18:24:13","slug":"properly-padding-spaces-and-tabs","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5819","title":{"rendered":"Properly Padding Spaces &#8211; and Tabs"},"content":{"rendered":"<p>As you can tell by the post title, part of the <em>sconvert<\/em> program I missed is to convert tab characters into HTML spaces. Like spaces, tabs output blanks that must be converted to the <code>&amp;nbsp;<\/code> code for proper output on a web page.<br \/>\n<!--more--><br \/>\nIn <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5812\">last week&#8217;s Lesson<\/a>, I told the riviting story of the <em>sconvert<\/em> utility, which I use to process program output for this blog. It replaces spaces with the <code>&amp;nbsp;<\/code> (non-breakable space) HTML code. This program automates the conversion for me, so I don&#8217;t manually replace the spaces in sample output, which is a pain and takes time.<\/p>\n<p>The first draft of the code, however, didn&#8217;t convert tabs. Adding tab detection and output routines requires updating the existing code. To avoid messy <em>if-else<\/em> decisions, I chose to use a <em>switch-case<\/em> structure to evaluate and convert streaming input:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_04_08-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_04_08-Lesson.c<\/a><\/h3>\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 = getchar();\r\n        if( ch==EOF )\r\n            break;\r\n        switch(ch)\r\n        {\r\n            case ' ':\r\n                printf(\"&amp;nbsp;\");\r\n                break;\r\n            case '\\t':\r\n                printf(\"&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;\");\r\n                break;\r\n            default:\r\n                putchar(ch);\r\n        }\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Immediately after grabbing a character from standard input, an <em>if<\/em> test checks for the <code>EOF<\/code>. When it floats in, the endless <em>while<\/em> loop is broken.<\/p>\n<p>The <em>switch-case<\/em> structure looks for twoitems: a space, or a tab. The <em>default<\/em> condition is to output the character input, <code>ch<\/code>. Otherwise, for the space or tab, one or four HTML non-breakable space codes (<code>&amp;nbsp;<\/code>) are output.<\/p>\n<p>This update to the code converts tabs into non-breakable spaces for HTML output. One thing the code doesn&#8217;t do is to calculate tab offsets.<\/p>\n<p>For example, I wrote a program that outputs the days of the week followed by a number value. Figure 1 shows the output as it appears on the screen:<\/p>\n<div id=\"attachment_5828\" style=\"width: 285px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5828\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/04\/0408-figure1.png\" alt=\"Screenshot\" width=\"275\" height=\"207\" class=\"size-full wp-image-5828\" \/><p id=\"caption-attachment-5828\" class=\"wp-caption-text\">Figure 1. Text output with tabs may not line up perfectly. (Tab stops set every eight positions.)<\/p><\/div>\n<p>Yes, the program output is ugly because tab stops in the terminal are set every eight column positions. Here is how the updated <em>sconvert<\/em> program interprets the output:<\/p>\n<p><code>Monday&nbsp;&nbsp;&nbsp;&nbsp;0<br \/>\nTuesday&nbsp;&nbsp;&nbsp;&nbsp;1<br \/>\nWednesday&nbsp;&nbsp;&nbsp;&nbsp;2<br \/>\nThursday&nbsp;&nbsp;&nbsp;&nbsp;3<br \/>\nFriday&nbsp;&nbsp;&nbsp;&nbsp;4<br \/>\nSaturday&nbsp;&nbsp;&nbsp;&nbsp;5<br \/>\nSunday&nbsp;&nbsp;&nbsp;&nbsp;6<\/code><\/p>\n<p>Oops, the results are different. The program merely translates tabs into non-breakable HTML space codes without consideration for actual tab stops on the terminal. To more accurately convert tabs, more manipulation is required in the code, which I cover in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The space character isn&#8217;t the only character that outputs blanks at the terminal. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5819\">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-5819","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\/5819","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=5819"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5819\/revisions"}],"predecessor-version":[{"id":5836,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5819\/revisions\/5836"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}