Ok, here's the story.
I am writing a substitution cipher which simply replaces any lower case letter with a predetermined number. But when I try to decode it using a decoder it gets stuck in an infinite loop. Can you figure out whats wrong with the decoder.
Encoder
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
int main(void)
{
int i,j;
char c[]="abcdefghijklmnopqrstuvwxyz ",t;
char d[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
FILE *fp;
fp=fopen("output.txt","wb+");
if(fp==NULL)
{
puts("Cannot open file");
exit(0);
}
while((t=getchar())!=EOF)
{
i=0;
while(c[i]!=t)
i++;
putc(d[i],fp);
}
fclose(fp);
}
Decoder
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
int main(void)
{
int i,j;
char c[]="abcdefghijklmnopqrstuvwxyz ",t;
char d[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
FILE *fp;
fp=fopen("output.txt","rb+");
if(fp==NULL)
{
printf("Cannot open file");
exit(0);
}
while((t=getc(fp))!=EOF)
{
i=0;
while(d[i]!=t)
i++;
fseek(fp,-1L,1);
putc(c[i],fp);
}
fclose(fp);
}