The Final Output to HTML Code

The final update to the sconvert function includes translating characters &, <, and > for proper HTML output. With this update to the code from last week’s Lesson, the conversion filter is complete and can be used to translate C program text output into HTML code that I can easily post on the web.

The update requires only these lines of code, added within the switch-case structure:

            case '&':
                printf("&amp;");
                offset++;
                break;
            case '<':
                printf("&lt;");
                offset++;
                break;
            case '>':
                printf("&gt;");
                offset++;
                break;

To view the full code, click here to visit my GitHub.

The output doesn’t bother translating the newline (\n) or return (\r) into the <br> code. Honestly, the reason it doesn’t do so is that I’ve never needed it. When I run output through the sconvert utility, I’ve never had an issue with newlines mistranslating. So while it makes sense to convert them from an HTML formatting perspective, I’ve not needed to do so.

This utility is one of my favorite types of things to code: a program that provides a solution to something that otherwise takes me a mountain of time. Most of these programs are just quick-and-dirty. In fact, I would say a majority of the programs I write for myself, to automate a task or perform some redundancy, are written in the shell scripting language. But for more complex programs, I enjoy sitting back and using my C skills to code a solution that saves me time and typing molecules. It’s handy, practical, and fun. That’s what I enjoy most about coding.

2 thoughts on “The Final Output to HTML Code

  1. If you wanted to streamline the code a bit, and make it easier to add replacements without adding more cases, you could create a pair of arrays mapping characters that need to be replaced with their replacement strings. Then if a character is in the first array replace it with the corresponding string in the second, else just output the original character. Hope that makes sense. Ideally some sort of associative array/dictionary might be better.

Leave a Reply