issue with letter shift direction in caesar cipher c program

user197932 Source

I've been working on a character shifting program that's supposed to shift lower and uppercase letters in a text file a given number of times left or right as selected by the user and then output the shifted letters to another text file. The program is to output any numbers without shifting them. An example of what the program should be able to do is shift the letters of text three letters to the right and if you fed the output back into the program and shift three letters to the left I should get the text I started with out of it. What I'm having a problem with is after the program shifts the text and I put the output back in and shift the opposite direction an equal number of times I don't get what I started with. I just can't figure out how to get the direction to go the right way. Someone told me to set the direction to 1 and -1, but the program still doesn't do the right thing. What am I doing wrong?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX_SHIFT 25
#define WRAP_ALPHABET 26
#define MIN_LOWER 97
#define MAX_LOWER 122
#define MIN_UPPER 65
#define MAX_UPPER 90
#define FNLEN 100


int main(void)
{
FILE *ptInFile;
FILE *ptOutFile;

int input;
short int shift;
short int direction;
char strFileNameIn[FNLEN];
char strFileNameOut[FNLEN];

printf("Enter name of file to be encoded.\n");
scanf("%s", strFileNameIn);

printf("Enter name of file to save output to.\n");
scanf("%s", strFileNameOut);

printf("Enter number to shift characters by:\n");
scanf("%hu", &shift);

direction = 1;
direction = -1;
shift = direction * shift;

ptInFile = fopen(strFileNameIn, "r");

ptOutFile = fopen(strFileNameOut, "w");


while ((ptInFile = fopen(strFileNameIn, "r")) == NULL)
{
    printf("%s is an invalid name.\n", strFileNameIn);
    printf("Please enter valid file name. ");
    scanf("%s", strFileNameIn);
}

while ((input = getc(ptInFile)) != EOF)
{


        if (isalpha(input))
        {

            if(islower(input))
            {
                input += shift;
                input++;

                if (input < MIN_LOWER)
                {
                    input += WRAP_ALPHABET;
                    input++;
                }
                if (input > MAX_LOWER)
                {
                    input -= WRAP_ALPHABET;
                    input++;
                }
            }
            else
            {
                if(isupper(input))
                {
                    input += shift;
                    input++;
                }
                if(input < MIN_UPPER)
                {
                    input += WRAP_ALPHABET;
                    input++;
                }
                if(input > MAX_LOWER)
                {
                    input -= WRAP_ALPHABET;
                    input++;
                }
            }



        }
        putc(input, ptOutFile);


}

fclose(ptInFile);
fclose(ptOutFile);

printf("\nPress enter to end the program.\n");
fflush(stdin);
getchar();

return 0;

}

cencryptionshiftcaesar-cipherletter

Answers

comments powered by Disqus