I am trying to build a program that will do a simple caesar cipher on a text file with single strings with no spaces on each line. For some reason, my cipher function is not shifting text and I am cutting off the strings at various lengths of characters. Can you see where I am messing up with my function call in the while loop?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define FILE_NAME "./infile.txt"
void caeser (char * ch, int shift)
{
int i = 0;
int len = strlen(ch);
while (ch[i] < len)
{
if (islower(ch[i]))
ch[i] = ((ch[i] - 'a' + shift) % 26 + 'a');
else
ch[i] = ((ch[i] - 'A' + shift) % 26 + 'A');
}i++;
printf("Caesar Cipher = %s\n", ch);
}
int main(void)
{
char * c = malloc( sizeof(char) * 1000);
FILE* fp = fopen (FILE_NAME, "r");
if (fp == NULL)
{
printf("Can't open %s\n", FILE_NAME);
exit(EXIT_FAILURE);
}
while(fgets(c, sizeof(c), fp) != 0)
{
printf("%s\n", c);
caeser(c, 1);
}
fclose(fp);
fp = NULL;
return 0;
}