I am encrypting text from a file and storing the newly encrypted message in a text file.
If the ith char is an uppercase letter, shift it to be the char x[i] away where x = {3, -1, 0, 2, -2}
If ith char is a lower case letter, shift it to be the char y[i] away where y = {2, 4, -1, -2}
If ith char is a digit, shift it to be the char z[i] away where z = {1, -1, 2}
If a char is a non-letter and non-digit, then it does not change
Also A + 1 would become Z, z - 1 becomes a, and 9 + 1 becomes 0
Example:
Hello! would have the H shift 3 further away (H becomes K), e would be shift 4 away (becoming i), l shifts -1 away (becoming k), the second l shifts -2 away (l becomes j), o shifts 2 away since we reached the end of the y array (o becomes q). and ! remains the same.
So far I have:
Code:
#include <stdio.h>
int main()
{
/* array for shifting an uppercase letter */
int x[] = {3, -1, 0, 2, -2};
/* tarray for shifting a lowercase letter */
int y[] = {2, 4, -1, -2};
/* array for shifting a digit */
int z[] = {1, -1, 2};
int *xNext = x;
int *yNext = y;
int *zNext = z;
FILE *fp1, *fp2; // file pointers
const int xArraySize = 5;
const int yArraySize = 4;
const int zArraySize = 3;
int totalChars = 0;
int iter = 0;
char inputTextArray[5000];
char inputFilename[30]; // Character Array to hold the users specified input file name
char currentCharacter; //current character input from the file
char outputFilename[30]; // holds the users specified output file name
char *textArrayPointer = inputTextArray;
printf("Enter the name of the file to be scanned: ");
scanf("%s", inputFilename); // Scans the input file specified by the user
fp1 = fopen(inputFilename, "r"); // open the file as read-only
printf("Enter the name of the file you wish to copy %s to: ", inputFilename); // ask user for outputFilename (copy of inputFilename)
scanf("%s", outputFilename);
fp2 = fopen(outputFilename, "w"); // open output file, write-only
while((currentCharacter = getc(fp1)) != EOF)
{
*textArrayPointer = currentCharacter;
textArrayPointer++;
}
while(*textArrayPointer != '\0')
{
/* Check for capital letters A through Z
A in ASCII is 65, and Z is 90 */
if (( *textArrayPointer >= 65 ) && ( *textArrayPointer <= 90 ))
{
iter = totalChars % xArraySize;
while(iter-- != 0)
{
xNext++;
}
*textArrayPointer = *textArrayPointer + *xNext;
if ( *textArrayPointer < 65)
{
*textArrayPointer += 26;
}
if ( *textArrayPointer > 90)
{
*textArrayPointer -= 26;
}
putc(*inputTextArray, fp2);
}
/* Check for lower case letters a through z
a in ASCII is 97, and z is 122 */
else if (( *textArrayPointer >= 97 ) && ( *textArrayPointer <= 122 ))
{
iter = totalChars % yArraySize;
while(iter-- != 0)
yNext++;
*textArrayPointer = *textArrayPointer + *yNext;
if ( *textArrayPointer < 97)
{
*textArrayPointer += 26;
}
if ( *textArrayPointer > 122)
{
*textArrayPointer -= 26;
}
putc(*inputTextArray, fp2);
}
/* Check for numbers 0 through 9
0 in ASCII is 48, and 9 is 57 */
else if (( *textArrayPointer >= 48 ) && ( *textArrayPointer <= 57 ))
{
iter = totalChars % zArraySize;
while(iter-- != 0)
zNext++;
*textArrayPointer = *textArrayPointer + *zNext;
if ( *textArrayPointer < 48)
{
*textArrayPointer += 26;
}
if ( *textArrayPointer > 57)
{
*textArrayPointer -= 26;
}
putc(*inputTextArray, fp2);
}
/* The criteria did not meet the previous
conditions. The character must be a symbol */
else
{
putc(*inputTextArray, fp2);
}
*textArrayPointer++;
totalChars++;
}
}
I get a run time error and I am not sure what I have done wrong. Any assistance would be much appreciated.