Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

Caesar Cipher

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
StephenBucholtz
Asked:
StephenBucholtz
  • 4
  • 3
1 Solution
 
ozoCommented:
The simplest patch seems to be

CaesarAlpha[]={"XYZABCDEFGHIJKLMNOPQRSTUVW" "XYZAB"};

// I added -cipheroffset. // cipherindex=26;


But a better method may be to learn about the % operator
0
 
ozoCommented:
if( CaesarAlpha[cipherindex+cipheroffset] == chartest){
         ch=PlainAlpha[cipherindex];
0
 
alexoCommented:
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';
}

That's all!

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

By the way, you asked the same question twice.  You can ask the admins to cancel one of the copies and you'll get it's points back.
0
 
alexoCommented:
Oops...  It's +23 and not +3.  Mea culpa...

0
 
ozoCommented:
alexo, StephenBucholtz could have canceled the extra question without needing admins help if you hadn't locked both of them.
0
 
alexoCommented:
Gaaak!  Sorry.  Didn't know that.  please accept my humble apologies.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now