Hey guys,
I was just messing around making a really simple encryption type program. Reads a file in, changes some letters around (makes no effort at case sensitivity), then outputs it to another file. I have given it a good try but can't seem to get the letters to change. I'm sure it's something stupid...but I can't see it right now.
Code:
/**************************************************************************************
A program to receive a text file, then use simple encryption method to encrypt it, then
another method to decrypt it***********************************************/
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 63
#define LBUFSIZE 64
int getWord(FILE *fp, char wbuf[], int c);
void output(char lbuf[]);
FILE *encrypt;
void main(void)
{
FILE *fp;
int c = 0;
char wbuf[SIZE];
char lbuf[LBUFSIZE];
encrypt = fopen("encrypt.dat", "w");
fp = fopen("file.txt", "r");
lbuf[0] = '\0';
c = getWord(fp, wbuf, c);
while(c != EOF)
{
//add 1 for space
if((strlen(lbuf) + 1 + strlen(wbuf)) > 62)
{
output(lbuf);
strcpy(lbuf, wbuf);
}
else
{
//enough space for this word
strcat(lbuf, " ");
strcat(lbuf, wbuf);
}
c = getWord(fp, wbuf, c);
}
if (c == EOF && strlen(lbuf) > 0)
{
output(lbuf);
}
fclose(fp);
fclose(encrypt);
}
int getWord(FILE *fp, char wbuf[], int c)
{
int i = 0;
if(isalpha(c))
{
letterSwitch(c);
wbuf[i] = c;
i++;
}
c = fgetc(fp);
while(c !=EOF && isalpha(c))
{
letterSwitch(c);
wbuf[i] = c;
i++;
c = fgetc(fp);
}
wbuf[i] = '\0';
while(c != EOF && !isalpha(c))
{
c = fgetc(fp);
}
return c;
}
void output(char lbuf[])
{
printf("%s\n", lbuf);
fprintf(encrypt, "%s\n", lbuf);
}
int letterSwitch(int c)
{
switch(c)
{
case 'a':
c = '!';
case 'e':
c = '@';
case 'i':
c = '#';
case 'o':
c = '%';
case 'r':
c = '^';
case 'b':
c = '&';
case 's':
c = '*';
case 't':
c = '(';
case 'v':
c = ')';
case 'p':
c = '_';
case 'l':
c = '+';
case 'n':
c = '=';
case 'm':
c = '[';
case 'g':
c = ']';
case 'c':
c = '?';
case 'y':
c = '<';
case 'q':
c = '>';
case 'k':
c = '/';
}
return c;
}
And there it is!