Solved

Caesar Cipher

Posted on 1997-12-16
1
1,311 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

822 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