Solved

Caesar Cipher

Posted on 1997-12-16
1
1,306 Views
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];
char PlainAlpha[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
char CaesarAlpha[]={"XYZABCDEFGHIJKLMNOPQRSTUVW"};
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:");
 scanf("%s",Infilename);
 printf("\nEnter Output Filename:");
 scanf("%s",Outfilename);
 printf("\nEnter 1 if you wish to encode");
 printf("\nEnter 2 if you wish to decode");
 printf("\nEnter your choice here:");
 scanf("\n%c",&caesercode);
   printf("Enter Offset index between 1-5: ");    //I prompt user
   scanf("\n%d",&cipheroffset);                //I store it to
cipheroffset
 caesercode=toupper(caesercode);

 if (((outfile=fopen(Outfilename,"w"))==NULL) ||
  ((infile=fopen(Infilename,"r"))==NULL))
 {
  printf("ERROR: Opening files\n");
  exit(-1);
 }
 while((ch=getc(infile))!=EOF)
 {
  if (isalpha(ch))
  {
   chartest=toupper(ch);
   cipherindex=0;
   if (caesercode == '1' )
   {
     while (cipherindex < 26)
     {
      if (PlainAlpha[cipherindex] == chartest)
      {
       ch=CaesarAlpha[cipherindex]+cipheroffset;  //I added
"+cipheroffset
       cipherindex=26;
      }
      else
       cipherindex = cipherindex + 1;
      }
   }
    else
    {
     while (cipherindex < 26)
     {
      if (CaesarAlpha[cipherindex] == chartest)
      {
        ch=PlainAlpha[cipherindex]-cipheroffset; // I added
-cipheroffset.
        cipherindex=26;
      }
      else
        cipherindex  = cipherindex + 1;
      }
    }
  }
 fprintf(outfile,"%c",ch);
 }
 printf("\nCheck OutFile for Deciphered Message");
 fclose(outfile);
 fclose(infile);
}

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

Thank you.

0
Comment
Question by:StephenBucholtz
1 Comment
 
LVL 11

Accepted Solution

by:
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';
      else
            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!

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Binary Bomb: Phase 4 func4 3 310
Which checksum is this? 7 139
Windows Updates failing due to Diskpart not configured correctly 8 106
What is atomic operation? 6 47
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

864 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

22 Experts available now in Live!

Get 1:1 Help Now