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 rowan on 11/12/11.
//  Copyright 2011 Rowan Watson. All rights reserved.
#include <iostream>
#include <string>
using namespace std;
char caesar( char );
int main()
{
    string input;
    do {
        cout << "Enter cipertext and press enter to continue." << endl;
        cout << "Enter blank line to quit." << endl;
        getline(cin, input);
        string output = "";
        for(int x = 0; x < input.length(); x++)
        {
            output += caesar(input[x]);
        }
        cout << output << endl;
    } while (!input.length() == 0);
}  //end main

char caesar( char c )
{
    if( isalpha(c) )
    {
        c = toupper(c); //use upper to keep from having to use two seperate for A..Z a..z
        c = (((c-65)+13) % 26) + 65;
    }
    //if c isn't alpha, just send it back.
    return c;
}

Comments

Posted On
Apr 17, 2012
Posted By
hiit

I like your fantastic web site, I was searching for this all over.
best regards

Posted On
May 11, 2012
Posted By
Oliver Keodalah

This helped me understand my homework assignment a lot. thanks

Posted On
Jul 17, 2012
Posted By
Janean Vear

Great article.

Posted On
Aug 20, 2012
Posted By
priyanka

i would jus like to say that u r a greeeeeeeaaaaaaaaat dad.:)

Posted On
Nov 26, 2012
Posted By
Raheeg

thank you alot for helping me to do my homework all the best

Posted On
Dec 08, 2012
Posted By
Helloworld

Please explain this part of the code:
“c = (((c-65)+13) % 26) + 65;”
:o

Posted On
Jul 09, 2013
Posted By
Taylor Murray

That was a very neat program and I loved it. Though, it was a bit confusing at first. This program look easier, if you may take a look: http://www.wncoder.com/2012/05/c-caesar-cipher-code.html

Posted On
Aug 24, 2013
Posted By
jeril

thanks a lot

Posted On
Oct 21, 2013
Posted By
c'john

Too bad your code doesn’t work.

Posted On
Apr 03, 2014
Posted By
rowan

Er… everyone else got it going. What’s not working about it?

Posted On
May 15, 2014
Posted By
*Carey*

This is seriously the kind of site that starting out C++ programmers need. i am able to code in C# and Delphi, but C++ has been giving me problems all semester. i would definitely subscribe to a site that could make my life easier in terms of understanding how to program things like Ceasars cipher in C++, since in my lectures all we learn is theory!

Thank you Rowan Watson!

Posted On
Sep 12, 2014
Posted By
jasonwynn10

If you are interested, I recommend cplusplus.com

Posted On
Aug 23, 2014
Posted By
Armen

this code not work,

Posted On
Sep 12, 2014
Posted By
jasonwynn10

Now can you make it store the data in a .txt file and decode it?

Leave a Reply



one + = 3

Login