I wrote this code for a Caesar cipher but am stuck at the end.
I have three function:
processFile() opens a file (congress.txt) and stores it in an array.
cipher() shifts all the letters up one letter.
outputCode() is supposed to print the new text in blocks of five, ten blocks per line.
That is where I am stuck. I am able to go to a new line every 50 characters but can't figure out how to get the characters in blocks of five.
My ouput:
DPOHSFTTTIBMMNBLFOPMBXSFTQFDUJOHBOFTUBCMJTINFOUPGS
FMJHJPOPSQSPIJCJUJOHUIFGSFFFYFSDJTFUIFSFPGPSBCSJEH
JOHUIFGSFFEPNPGTQFFDIPSPGUIFQSFTTPSUIFSJHIUPGUIFQF
PQMFQFBDFBCM@UPBTTFNCMFBOEUPQFUJUJPOUIFHPWFSONFOUG
PSBSFESFTTPGHSJFWBODFT
Desired output:
DPOHS FTTTI BMMNB LFOPM BXSFT QFDUJ OHBOF TUBCM JTINF OUPGS
FMJHJ POPSQ SPIJC JUJOH UIFGS FFFYF SDJTF UIFSF PGPSB CSJEH
JOHUI FGSFF EPNPG TQFFD IPSPG UIFQS FTTPS UIFSJ HIUPG UIFQF
PQMFQ FBDFB CM@UP BTTFN CMFBO EUPQF UJUJP OUIFH PWFSO NFOUG
PSBSF ESFTT PGHSJ FWBOD FT
Any suggestions?
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
FILE *csis;
//Functions to be used.
void processFile(int *store);
void cipher(int *store, int *code);
void outputCode(int *code);
int main()
{
csis = fopen("csis.txt", "w");
int store[300], code[300], i;
//Order of functions to be called.
processFile(store);
cipher(store, code);
outputCode(code);
fclose(csis);
return 0;
}
//Function to open the Congress text
//file and set it up for cipher.
void processFile(int *store){
int i;
char a = 0;
FILE *f = fopen("congress.txt", "r");
//Stores lowercase letters as uppercase, etc.
for (i = 0; !feof(f) && i<299;){
fscanf(f, "%c", &a);
if (a <= 'Z' && a >= 'A'){
store[i] = a;
i++;
}
if (a <= 'z' && a >= 'a'){
store[i] = a - 32;
i++;
}
}
store[i]='\0';
}
//Function to execute the shift by 1 letter.
void cipher(int *store, int *code){
int i;
for (i = 0; store[i] != 0; ++i){
if (store[i] <= 'X' && store[i] >= 'A'){ //Tests to see if the letter is between A and X.
code[i] = (char)(store[i] + 1); //Shifts letter by one characters.
}
if (store[i] >= 'Y' && store[i] <= 'Z'){
code[i] = (char)(store[i] - 25); //Shifts Y and Z.
}
}
for(; i<300; i++) code[i]=0; //Pad with zeros.
}
//Function to output the final encoded message in blocks.
void outputCode(int *code){
int i, a, b;
for (a = 0; code[a] != 0; ++a){
//New line every 50 characters.
if (!(a % 50)){
printf("\n");
fprintf(csis, "\n");
}
printf("%c", code[a]);
fprintf(csis, "%c", code[a]);
}
}