Jim Lawless' Blog


A Simple ROT13 Macro

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

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

del_icio_us Save to del.icio.us
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


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

Preserving my Favorite HN Links

An Interview with the Creator of the BDS C Compiler

A Simple Associative Array Library in C

Yet Another Enhanced Echo Command

Rhino JavaScript to EXE with launch4j

A DSL in JavaScript

Command-Line Image Format Conversion

Book Review : Using Google App Engine

Blog Posts by Category

Auto Save Clipboard Images Redux


Full List of Posts

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


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer