Jim Lawless' Blog


A Simple ROT13 Macro

Originally published on: Wed, 22 Jul 2009 23:26:56 +0000

Please note! If you're having difficulties compiling the C source code presented below, please see my post: Compiling C from the Command Line with Pelles C

Here's a simple C routine written as a preprocessor macro that will perform ROT13 encoding on a single character.


#define rot13(x) isalpha(x) ? tolower(x)>'m' ? x-13 : x+13 : x

If your C compiler's version of tolower expands to a nested ternary expression, then the above macro may generate some tricky-to-read code out of the preprocessor phase.

Here's a simple program to read a string from stdin and ROT13 encode it using the above macro.


//
// A simple ROT13 encoder macro
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.


// sorry about the includes below. Something in the Wordpress
// source code widget is garbling up the
// source. Please be sure to put angle brackets around the
// header file names
#include stdio.h
#include ctype.h

#define rot13(x) isalpha(x) ? tolower(x)>'m' ? x-13 : x+13 : x

int main() {
   int c;
   while((c=fgetc(stdin))!=-1)
      fputc(rot13(c),stdout);
   return 0;
}

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: Removing IE Popups in C
Next post:Blog Posts by Category


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Backlinks to my Blog Posts

Screen Captures with Java and Clojure

Envy

A DSL in JavaScript

Tracing XSLT with a Tiny Java Web Server

Auto Save Images from the Clipboard

Flirting with Forth

An SMTP Server Simulator in Perl

Safe Scripting with Scroll Lock and Caps Lock

Embedding JavaScript in a Batch File


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service