C++ Caesar Cipher Program
I’ve never had any formal programming training, even though my day job and my side business require me to know programming and database concepts. I’ve written scripts and programs in various languages including PHP, Javascript, VB.Net, ActionScript 3, and others. Sometimes I like to hack through programming tasks for fun and this time a tried my hand at some C++. Fun!
I started off wanting to build a simple program that would encode and decode a simple scheme for sending secret messages that my son and I came up with. He’s 7 and I thought it would be a good idea to get him interested in different computing concepts at an early age. Even if he never grows up to be a cryptographer or programmer, just the idea of cryptography is something that can help get him thinking about things in new ways.
I was able to complete my goal in VB.Net in about 30 minutes and another 15 to really make a polished windows form application that did exactly what I wanted. However, I found c++ to be a bit more complicated (albeit a bit more powerful) and I kinda got lost on the way to where I really wanted to go.
So, rather than actually complete the program I wanted I ended up making a simple Caesar cipher program in c++. If you’re not familiar with the Caesar Cipher, basically it where you shift the alphabet over a predetermined amount and use that to encrypt your message - so, an A shifted 3 letters would be a D.
It took me a little while to find an example code to get going on the Caesar project in c++, so I’m going to post mine here. The ones I did find were either half finished, overly complex, or wrong. I think that the Caesar Cipher might be a popular homework assignment for some high school or college courses; consequently, some curmudgeons feel that we shouldn’t post working code since that would, well, I’m not really sure what it would do or prevent. I almost always start with some example code or code I’ve previously written before I begin just about any project. Heck, how many times should I be expected to type out the same database connection code?!!
I used Xcode 4 to write and compile the program, so those of you using Xcode 4 should have no issues; either way, simple code like this is generally portable to any standard c++ compiler.
So with out further ado, here is the c++ code for the Caesar Cipher - very simple!
// main.cpp
// Caesar Cipher
//
// Created by c0de.icu on 11/12/11.
// Copyright 2011 c0de.icu. All rights reserved.
#include <iostream>
#include <string>
using namespace std;
char caesar( char );
int main()
{
string input;
do {
c<< "Enter cipertext and press enter to continue." << endl;
c<< "Enter blank line to quit." << endl;
getline(cin, input);
string output = "";
for(int x = 0; x < input.length(); x++)
{
output += caesar(input[x]);
}
c<< out << endl;
} while (!input.length() == 0);
} //end main
char caesar( char c )
{
if( isalpha(c) )
{
//use toupper keep from having to use two seperate for A..Z a..z
c = toupper(c);
c = (((c-65)+13) % 26) + 65;
}
//if c isn't alpha, just send it back.
return c;
}