Question
Here is the code I have been working on. The question is in the attachment. Could someone please help me fix it so that it
Here is the code I have been working on. The question is in the attachment. Could someone please help me fix it so that it will run without errors? Thank you!
//Gabrielle
#include
#include
//Function prototypes
void processFile(int *store);
void cipher(int *store, int *code);
void outputCode(int *code);
int main()
{
int store[300], code[300], i;
processFile(store);
cipher(store, code);
outputCode(code);
printf(" ===== ");
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 *fp = fopen("congress.txt", "r");
for (i = 0; !feof(fp) && i<299;){
fscanf(fp, "%c", &a); //store character in a
if (a <= 'Z' && a >= 'A'){ //store uppercase letters
store[i] = a;
i++;
}
if (a <= 'z' && a >= 'a'){ //store lowercase letters as uppercase
store[i] = a - 32;
i++;
}
}
store[i]='\0';
return 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] + 2); //shifts letter by two characters
}
if (store[i] >= 'Y' && store[i] <= 'Z'){
code[i] = (char)(store[i] - 24); //shifts Y and Z to A or B respectively
}
}
for(; i<300; i++) code[i]=0; // pad with zeros
return 0;
}
//Function to output the final encoded message in blocks.
void outputCode(int *code)
{
int i, a, b;
for (a = 0; code[a] != 0; ++a){
if (!(a % 50)){ //makes a newline every 50 characters
printf(" ");
}
for (b = 0; code[a] != 0 && b <= 5; ++b){ //prints chunks of 5 characters then makes a space
printf("%c", code[a++]);
}
printf(" ");
}
return 0;
}
}
[me@alarmpi HW 6 8 10]$ gcc hw8.c
hw8.c: In function 'processFile':
hw8.c:46:12: warning: 'return' with a value, in function returning void
return 0;
^
hw8.c:27:6: note: declared here
void processFile(int *store)
^~~~~~~~~~~
hw8.c: In function 'cipher':
hw8.c:65:12: warning: 'return' with a value, in function returning void
return 0;
^
hw8.c:51:6: note: declared here
void cipher(int *store, int *code)
^~~~~~
hw8.c: In function 'outputCode':
hw8.c:85:8: warning: 'return' with a value, in function returning void
return 0;
^
hw8.c:71:6: note: declared here
void outputCode(int *code)
^~~~~~~~~~
hw8.c: At top level:
hw8.c:88:1: error: expected identifier or '(' before '}' token
}
Cipher Homework Assignment
The purpose of this short assignment is to give you some practice using characters and Strings. You will create a program, cipher.c, which will encode a message by shifting each letter some number of places. Thus, if the shift is 2, then A becomes C, B becomes D, and so on. Like this: 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 letter contains the value A, then A + 2 gives the integer results 67, which you can turn back into a character by saying (char) (letter + 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 subtract 26 (so the result is Z + 2 26, or Z 24), and this will give you B. This also means that if you encode a message with a shift of n, you can decode it with another shift of 26 n. 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 to the correct amount, and put the encoded letters into a null-terminated array of characters. Finally, 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. Remember that the main() function should appear as the first function in the program. Be sure to use function prototypes for each of the functions that are used in your program. For example, given a shift of 1, the program should turn this message: Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B into this: 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 CMZUP BTTFN CMFBO EUPQF UJUJP OUIFH PWFSO NFOUG PSBSF ESFTT PGHSJ FWBOD FT
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started