Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CipherLab se of this short assignment is to give you some practice using The pur

ID: 3762475 • Letter: C

Question

CipherLab se of this short assignment is to give you some practice using The purpose of this s characters an a messbocomes C, B becomes D, and so on. Like this: The Plars and Strings. You will create a program, cipher.c, which will encode e by shifting each letter some number of places. Thus, if the shift is 2, a message by sh then A becomes C, B ABC D E Surprisingly, you can do this by simply doing arithmetic with characters, but you should reassure C that the result is a character using a cast. If, for example, a you can turn back into a character by saying (char) (letter + 2), giving the etter conta contains the value 'A, then 'A' + 2 gives the integer result 67, which can turn back into a character by saying (char) (letter t 2), giving the value 'c Unfortunately, (char) ('Z" + 2) does not give you the letter 'B' (you can see why from the picture above). But if you realize you went past 'Z' ,you can you 'B subtract 26 (so the result is'Z' 2 - 26, or 'z' 24), and this will give This also means that if you encode a message with a shift of n, you can decode t with another shift of 26 - n. This also means that f you encode a messagewith a shit of n you can decode Your program should first call a function, processFile(), which reads a message from a file (congress.txt) into a very large character array. The function should convert all of the letters into uppercase characters. You may discard all the punctuation marks, digits, blanks, and anything else from the input string. Your program should then accept the amount to shift as an input to a function, cipher (). The input parameter should be 13, although for your testing purposes, you can simplify things by using a shift of 1. Your function should encode each letter by shifting it the correct amount, and put the encoded letters nto nul-terminated array of characters. ihially, you should create a function, outputCode (), which outputs the final encoded message in blocks of five letters, ten blocks per line. The last line may be shorter than five blocks, and the last block may be shorter than five letters. CSCI 112 Progra Programming Fundamentals I Page 355

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

char * cipher(char *input, int shift) {

int i=0;

char *out = NULL;

while (input[i]!='') {

out = (char *)realloc(out, sizeof(char)*(i+1));

out[i] = (input[i]-'A'+shift)%26 + 'A';

i++;

}

return out;

}

void outputCode(char *input) {

FILE *f = fopen("csis.txt", "w");

int i=0;

int count=0;

while (input[i]!='') {

printf("%c", input[i]);

fprintf(f, "%c", input[i]);

i++;

  

if (i%5==0) {

printf(" ");

fprintf(f, " ");

}

if (i%50==0) {

printf(" ");

fprintf(f, " ");

}

}

}

char * processFile() {

FILE *f = fopen("congress.txt", "rb");

fseek(f, 0, SEEK_END);

long fsize = ftell(f);

fseek(f, 0, SEEK_SET);

  

char *string = (char *)malloc(fsize + 1);

fread(string, fsize, 1, f);

return string;

}

int main(void){

  

char *input = processFile();

char *output = cipher(input, 13);

outputCode(output);

  

printf(" ");

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote