Solved

Caesar Cipher

Posted on 1997-12-16
7
469 Views
Last Modified: 2008-03-10
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
  • 4
  • 3
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1256692
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
 
LVL 84

Expert Comment

by:ozo
ID: 1256693
if( CaesarAlpha[cipherindex+cipheroffset] == chartest){
         ch=PlainAlpha[cipherindex];
0
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1256694
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:alexo
ID: 1256695
"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
 
LVL 11

Expert Comment

by:alexo
ID: 1256696
Oops...  It's +23 and not +3.  Mea culpa...

0
 
LVL 84

Expert Comment

by:ozo
ID: 1256697
alexo, StephenBucholtz could have canceled the extra question without needing admins help if you hadn't locked both of them.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1256698
Gaaak!  Sorry.  Didn't know that.  please accept my humble apologies.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

707 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