{"id":5812,"date":"2023-04-01T00:01:05","date_gmt":"2023-04-01T07:01:05","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5812"},"modified":"2023-04-08T09:43:57","modified_gmt":"2023-04-08T16:43:57","slug":"properly-padding-spaces","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5812","title":{"rendered":"Properly Padding Spaces"},"content":{"rendered":"<p>I&#8217;ve written two programs specifically for this blog. The first converts a C source code file into HTML. The second translates program output into HTML. Both of these programs are time-savers, helping me prepare and present the code and output without having to hand-code everything.<br \/>\n<!--more--><br \/>\nThe source code to HTML program is called <em>chtml<\/em>, for C-to-HTML. It scans for line breaks, tabs, spaces, comments, ampersands, and less-than\/greater-than characters in the text. Each of these is converted to an HTML code. The program also converts the filename and adds the GitHub link for me. The result is output as text, which I copy-paste into the post.<\/p>\n<p>The <em>chtml<\/em> program saves me from having to convert all the coding myself and it catches things such as <code>&lt;stdio.h&gt;<\/code>, which is often consumed as an HTML tag.<\/p>\n<p>I wrote <em>chtml<\/em> back in 2019 and have updated it a few times since, most recently just last year. But I was missing its companion program, which I coded only a few months back. This program, <em>sconvert<\/em>, is a filter that consumes a program&#8217;s text output and converts it into HTML code.<\/p>\n<p>This issue <em>sconvert<\/em> resolves are spaces, two or more of which are ignored in HTML. For example, if the code outputs a matrix as a table, spaces line up the columns. Here is how such output looks when pasted into an HTML document:<\/p>\n<p><code>  5   8   3       8  10   9      13  18  12<br \/>\n  5  10   4       6   2   9      11  12  13<br \/>\n  6   2   1       1   4   9       7   6  10 <\/code><\/p>\n<p>Here is how it looks when I covert it to HTML, replacing the spaces with <code>&amp;nbsp;<\/code> (non-breakable space) tags:<\/p>\n<p><code>&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;10&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;13&nbsp;&nbsp;18&nbsp;&nbsp;12&nbsp;<br \/>\n&nbsp;&nbsp;5&nbsp;&nbsp;10&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11&nbsp;&nbsp;12&nbsp;&nbsp;13&nbsp;<br \/>\n&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;10&nbsp;<\/code><\/p>\n<p>Big difference!<\/p>\n<p>Before I wrote <em>sconvert<\/em>, I had to manually replace the spaces with <code>&amp;nbsp;<\/code> tags to ensure everything lined up. The <em>sconvert<\/em> program saves me oodles of time performing the conversion for me. Here it is, presented and converted thanks to the <em>chtml<\/em> program I wrote:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_04_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_04_01-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        if( ch==' ' )\r\n            printf(\"&amp;nbsp;\");\r\n        else\r\n            putchar(ch);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This filter is a simple replacement. The <em>while<\/em> loop spins eternally, <code>while(1)<\/code>. The statement <code>ch = getchar()<\/code> fetches characters from standard input.<\/p>\n<p>Remember that variable <code>ch<\/code> must be an <em>int<\/em> in order to detect the end-of-file marker, <code>EOF<\/code>. This test is performed to terminate the <em>while<\/em> loop:<\/p>\n<p><code>if( ch==EOF )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;break;<\/code><\/p>\n<p>The second <em>if<\/em> test checks for the space character, <code>if( ch==' ' )<\/code>. When found, the <code>&amp;nbsp;<\/code> HTML character code text is output. Otherwise, the character read is output, <code>putchar(ch);<\/code>.<\/p>\n<p>To capture the program&#8217;s output, I use a command like:<\/p>\n<p><code>$ a.out | sconvert<\/code><\/p>\n<p>The properly-formatted text, with the spaces converted for me, is then copied and pasted into a blog post.<\/p>\n<p>After using this program for a few weeks, I discovered a flaw. Can you guess what it is? It&#8217;s not that the output can contain less-than, greater-than, or ampersand characters. It&#8217;s more subtle than that. In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5819\">next week&#8217;s Lesson<\/a> when I reveal the flaw and the updated <em>sconvert<\/em> utility.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thousands spaces on a web page appear as a single space, unless you use the secret HTML character code. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5812\">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-5812","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\/5812","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=5812"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5812\/revisions"}],"predecessor-version":[{"id":5846,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5812\/revisions\/5846"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}