Download this source file
// ROT13 Encryption/Decryption. This program reads characters
// from the standard input, rotates alphabetic characters by
// adding 13 mod 26 to them, and outputs the result to the
// standard output. Re-encrypting a file results in the original
// plaintext file. Typical usage in DOS: rot13 file.rot
//
// Program notes: This program uses simple stream IO. Note the
// expression in the while loop ("cin.get(ch)"); this fetches the
// next input character from the standard input into the variable ch,
// and returns as its value 1 (or TRUE) if the operation suceeded, and
// zero (or FALSE) on EOF (or error). Although "cin >> ch" looks simpler,
// there is a difference; this form of input ignores (skips) all white
// space characters. In this program we want the output to be exactly
// the same as the input except for the encryption, so it is important
// to NOT skip white space. (cin.get() doesn't skip white space.)
// Written by Wayne Pollock, Tampa FL, 1999
#include
#include // aka , for isupper() and islower() functions.
int main ()
{
char base, ch;
while ( cin.get( ch ) )
{ if ( isupper( ch ) )
base = 'A';
else if ( islower( ch ) )
base = 'a';
else
{ cout << ch; // Not a letter, so just sent it out.
continue;
}
ch -= base;
cout << char( (ch + 13) % 26 + base );
}
return 0;
}