Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Caesar Cipher

Posted on 1997-12-16
1
Medium Priority
?
1,318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 11

Accepted Solution

by:
alexo earned 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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…
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.
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.

705 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