{"id":14507,"date":"2026-08-17T01:00:00","date_gmt":"2026-08-17T05:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=14507"},"modified":"2026-08-17T01:53:16","modified_gmt":"2026-08-17T05:53:16","slug":"hide-text-with-this-handy-program","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=14507","title":{"rendered":"Hide text with this handy program"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"14507\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p class=\"wp-block-paragraph\">The <strong>rot13<\/strong> \u201cencryption\u201d program is one of the first programs that new developers learn to write. Requiring only one variable, the majority of the program is an exercise in how to use the <strong>if<\/strong> statement to compare values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I first encountered rot13 when I was an undergraduate student in the early 1990s. If you posted online and gave spoilers to a TV show or movie, it was polite to hide the spoiler using rot13. If the other person didn\u2019t mind seeing the spoiler, they used rot13 to view your answer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The rot13 program is very simple to write in the C programming language, so let\u2019s write our own version here:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The rules for rot13 are fairly straightforward: <em>Rotate any letters in the input by 13 positions in the alphabet.<\/em> More practically, if it\u2019s a letter between A and M, replace it with a letter from N to Z. Or if it\u2019s a letter between N and Z, replace it with a letter from A to M. The same method applies to lowercase letters, too.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A neat feature of rot13 is that you can apply the method a second time to un-hide the message, because the English language uses 26 letters. Using rot13 twice always gives you the original message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The core of the rot13 program evaluates a message one letter at a time. For each letter, if it\u2019s between A\u2013M or a\u2013m, the program prints a new letter that\u2019s 13 positions further down the alphabet. Using ASCII, you can just <em>add 13<\/em> to the original letter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        if (((c &gt;= 'A') &amp;&amp; (c &lt;= 'M'))\n            || ((c &gt;= 'a') &amp;&amp; (c &lt;= 'm'))) {\n            putchar(c + 13);\n        }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the letter is between N\u2013Z or n\u2013z, then the program replaces it with a letter that\u2019s 13 positions \u201cup\u201d in the alphabet:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        else if (((c &gt;= 'N') &amp;&amp; (c &lt;= 'Z'))\n                 || ((c &gt;= 'n') &amp;&amp; (c &lt;= 'z'))) {\n            putchar(c - 13);\n        }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For any other character, such as punctuation, a number, or a special symbol, the program simply prints that character as-is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        else {\n            putchar(c);\n        }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">All that is left is adding some code to read data one letter at a time. For this, we could use a neat feature of the C programming language to read a character with <code>getchar<\/code>, store it, and evaluate it at the same time (using <code>while ((c = getchar()) != EOF) { }<\/code>) but I think the program will be easier for new programmers to understand if I use a <code>do<\/code> loop instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nint main()\n{\n    int c;\n\n    do {\n        c = getchar();\n\n        if (c != EOF) {\n            if (((c &gt;= 'A') &amp;&amp; (c &lt;= 'M'))\n                || ((c &gt;= 'a') &amp;&amp; (c &lt;= 'm'))) {\n                putchar(c + 13);\n            }\n            else if (((c &gt;= 'N') &amp;&amp; (c &lt;= 'Z'))\n                     || ((c &gt;= 'n') &amp;&amp; (c &lt;= 'z'))) {\n                putchar(c - 13);\n            }\n            else {\n                putchar(c);\n            }\n        }\n    } while (c != EOF);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you save this program as <code>rot13.c<\/code> and compile it using a C compiler (such as GCC on Linux) you will get a program called <strong>rot13<\/strong> that reads characters one at a time and \u201crotates\u201d letters by 13 positions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gcc -o rot13 rot13.c<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the program<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s give the program a simple test case to see that it works. We know that any letter from A\u2013M will be translated to N\u2013Z. That means if we give it the input <code>Abc<\/code> we should expect the output <code>Nop<\/code>. But any other non-letter character will remain untouched, such as <code>123<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo Abc123 | .\/rot13\nNop123<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can also send a simple message through the <strong>rot13<\/strong> program to see that the new message is hidden:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo Hello, World! | .\/rot13\nUryyb, Jbeyq!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And if we use <strong>rot13<\/strong> twice, we will see the original message:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo Hello, World! | .\/rot13 | .\/rot13\nHello, World!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, we can use the program to hide the spoiler to a TV show or movie, which we can later copy and paste into an email message or blog post:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo the butler did it. | .\/rot13\ngur ohgyre qvq vg.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And the message that I post online might look like this:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">I was very disappointed in the latest Sherlock Holmes movie, because it was obvious: gur ohgyre qvq vg.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn to write the classic rot13 program for yourself<\/p>\n","protected":false},"author":33,"featured_media":2700,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[100,150],"tags":[104,152],"class_list":["post-14507","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line","category-programming","tag-command-line","tag-programming"],"modified_by":"Jim Hall","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/14507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14507"}],"version-history":[{"count":3,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/14507\/revisions"}],"predecessor-version":[{"id":14554,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/14507\/revisions\/14554"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2700"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}