Caesar Cipher

Posted on 1997-12-16
Last Modified: 2008-02-01
I've written this program originally (with some help) to hardcode an
offset of one to the  alphabet to encode a message.  But I need to
include the option to an offset of 1-5.  The program worked and still
does.  But both the encode and decode use other then alpha characters
still.  Especially when I use a word with 'z' in it.

Please help me fine tune.

I've make remarks to the areas where I've attempted to provide usser
entered offset.

//Stephen Bucholtz
//c Programming
#include <stdio.h>
#include <stdlib.h>

FILE *infile, *outfile;
char Infilename[23];
char Outfilename[23];
int cipheroffset=0;  //Here I declare it
void main ( void )
   int cipherindex=0;
 char chartest;
 int ch;
 char caesercode;

 printf("The Caesar Cipher Program");

 printf("\nEnter Input Filename:");
 printf("\nEnter Output Filename:");
 printf("\nEnter 1 if you wish to encode");
 printf("\nEnter 2 if you wish to decode");
 printf("\nEnter your choice here:");
   printf("Enter Offset index between 1-5: ");    //I prompt user
   scanf("\n%d",&cipheroffset);                //I store it to

 if (((outfile=fopen(Outfilename,"w"))==NULL) ||
  printf("ERROR: Opening files\n");
  if (isalpha(ch))
   if (caesercode == '1' )
     while (cipherindex < 26)
      if (PlainAlpha[cipherindex] == chartest)
       ch=CaesarAlpha[cipherindex]+cipheroffset;  //I added
       cipherindex = cipherindex + 1;
     while (cipherindex < 26)
      if (CaesarAlpha[cipherindex] == chartest)
        ch=PlainAlpha[cipherindex]-cipheroffset; // I added
        cipherindex  = cipherindex + 1;
 printf("\nCheck OutFile for Deciphered Message");

Please make response to my email address so I can access this at work.

Thank you.

Question by:StephenBucholtz
1 Comment
LVL 11

Accepted Solution

alexo earned 100 total points
ID: 1256691
You don't need the arrays at all!

Caesar's cypher is just shifting the characters by a given offset.

You can replace the main loop with:

if (isalpha(ch))
      chartest=toupper(ch) - 'A';
      if (caesercode == '1')
            ch = (chartest + offset) % 26 + 'A';
            ch = (chartest - offset + 26) % 26 + 'A';

When "offset" equals to your cipheroffset + 3 (because the initial shift in CaesarAlpha[] is 3 characters).

That's all!


Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now