I am trying to program an encrptor and decryptor. My problem is a pointer that I assigned value first still has the same value as the second. I tried to use free but the problem is still the same. For example, I typed aslkdjlasc(sample only) and the output would be: helloworld
I try to run the program again, then I type daskjda the output would be like doctorxRLD
RLD is from the past value of this pointer. It's supposed to be 7 characters only, but since helloworld is 10, the first 7 letters are replaced by the encryption but the last 3 characters are still printed.
What do I do?
UPDATE: HERE IS PART OF THE CODE:
void encrypt(char *crypt)
{
char *plaintext,*encryption,slash=0x2F;
int i,j,k,flags,f;
encryption=(char *)malloc(sizeof(int));
plaintext=(char *)malloc(sizeof(int));
printf("Enter plaintext, spaces,commas,and dots should be represented as /:\n");
scanf("%s",&*plaintext);
for(i=0;i<strlen(plaintext);i++)
{
j=1;flags=0;
while(j<53 && flags==0)
{
if(plaintext[i]==slash)
{
encryption[i]=slash;
flags=1;
}
if(plaintext[i]==crypt[j])
{
encryption[i]=crypt[j-1];
flags=1;
}
k=j+2;
j=k;
}
}
printf("%s",encryption);
free(encryption);
free(plaintext);
getch();
}
HERE IS THE MAIN
main()
{
char c;
int timer;
char crypt[53]="***i have hidden my encryption code***";
clrscr();
printf("Press e to encrypt, d to decrypt, ESC to exit.\n");
c=getch();
switch(c)
{
case(0x1b):
exit(0);
break;
case(0x64):
decrypt(crypt);
break;
case(0x65):
encrypt(crypt);
break;
default:
printf("INVALID. FORCE EXIT IN 3 SEC0NDS.");
delay(3000);
exit(0);
}
getch();
}