[Solved] C Programming Caesar Cipher Instructions Caesar Cipher One Oldest Widely Known Encryption Q37176314
C Programming
________________________________________CaesarCipherInstructions_________________________________________
The Caesar Cipher isone of the oldest and most widely known encryption algorithms. Yourgoal is to code this using C Strings. You will write your code in Cfor this project and submit it as a .c file.
Program Flow
- Prompt the user for an input file name. The name must have a.txt suffix.
- Open the file for read; if the open fails, output an errormessage and exit the program.
- Prompt the user for an output file name prefix. This stringwill be concatenated to the beginning of the input file name tocreate the output file name.
- Open the output file for write; if the open fails, output anerror message and exit the program.
- Prompt the user for an encryption key. This will be read as anint and will determine the number of characters to shift in thecipher operation.
- Read one line at a time from the input file.
- Apply the cipher operation on the string.
- Write the enciphered line to the output file.
- After the input file has been processed, close the files andend the program.
Output file name
Use strcpy and strcatto build the output filename from the prefix and the input filename. For example, if the input filename is message.txt and theprefix is enc-, then the output file name should beenc-message.txt.
Reading in an int
Use scanf to read inthe integer key; don’t forget to put the & in front of the intargument in the call to scanf, to pass the address of theint variable.
Reading in the text file
Use a maximum buffersize of BUFFER_SIZE.
Use fgets in a whileloop to read in the file a line at a time. Remember that the callreturns “true” if the read was successful, or “false” otherwise, socan be used as the condition of the loop, so that the loop exitsonce there is no more data to be read.
The Functions
// Returns true (1) if `str` ends in “.txt”, else false (0)int validFileSuffix(char str[]);
- Check that the last four characters of the string are .txt.C does not have a primitive bool type, so return 1for true and 0 for false.
// Returns the given character shifted by the given amountint shiftChar(int ch, int shift);
- Examine the given character (which is passed in as anint).
- If the character is not A-Za-z (use isalpha) then return itunchanged.
- Otherwise we need to shift the character by the given amount.But care is needed to make sure that the shift “wraps” correctly.
- If the letter is lowercase:
- if its value plus the shift is greater than ‘z’, subtract 26 tobring it back into the a-z range.
- if its value plus the shift is less than ‘a’, add 26 to bringit back into the a-z range.
- Similar care needs to be taken for uppercase letters.
- If the letter is lowercase:
// Shifts all alphabetic characters by the given amountvoid shiftStr(char str[], int shift);
- Loop through each of the characters in the given string, andapply the given shift (by calling shiftChar).
// Reads in a file, enciphers with the given key, and writes the resultvoid encipher(FILE* inFile, FILE* outFile, int key);
- Assume both files are already open.
- Use a buffer of size BUFFER_SIZE.
- Use a while loop with fgets to read a line at a time from theinput file.
- Call shiftStr to perform the required shift.
- Write the shifted string to the output file with fprintf.
Sample Output
Input file : lazydog.txt
Lazy Dog========The quick brown fox jumped over the lazy dog.1 fish, 2 fish, Red fish, Blue fish!
Program Run…
Enter input file name (*.txt): lazydogError: *.txt required.Enter input file name (*.txt): lazydog.txtEnter output file prefix: enc-Enter key (integer): 7Encrypting…Done.
Output file : enc-lazydog.txt
Shgf Kvn========Aol xbpjr iyvdu mve qbtwlk vcly aol shgf kvn.1 mpzo, 2 mpzo, Ylk mpzo, Isbl mpzo!
Program Run…
Enter input file name (*.txt): enc-lazydog.txtEnter output file prefix: decoded-Enter key (integer): -7Encrypting…Done.
Output file : decoded-enc-lazydog.txt
Lazy Dog========The quick brown fox jumped over the lazy dog.1 fish, 2 fish, Red fish, Blue fish!
____________________________________lazydog.txt__________________________________________________________
Lazy Dog
========
The quick brown fox jumped over the lazy dog.
1 fish, 2 fish, Red fish, Blue fish!
________________________________________orders.txt______________________________________________________
Send three and four-pence, we’re going to a dance!
What?
Send reinforcements, we’re going to advance!
Oh!
_______________________THE GIVEN CODE TOUSE__________________________________________________________
//———————————————————————–
// caesar-cipher.c
//———————————————————————–
#include <stdio.h>
#include <ctype.h>
#include <string.h>
//———————————————————————–
// Function prototypes
// Returns true (1) if `str` ends in “.txt”, else false (0)
int validFileSuffix(charstr[]);
// Returns the given character shifted by the given amount
int shiftChar(int ch,int shift);
// Shifts all alphabetic characters by the given amount
void shiftStr(char str[],int shift);
// Reads in a file, enciphers with the given key, and writes theresult
void encipher(FILE *inFile, FILE *outFile,int key);
//———————————————————————–
// Global constants
const int BUFFER_SIZE = 2048;
//———————————————————————–
// Main program
int main() {
char inName[BUFFER_SIZE];
// TODO (0): declare strings (character arrays) for outName andoutPrefix
int key;
// TODO (1): Prompt for, and read in, input filename
// TODO (2): Validate file name
// while ( …not valid… ) {
// Error message
// Prompt for, and read in, input file name
// }
// TODO (3): Open input file for read (exit program iferror)
// TODO (4): Prompt for, and read in, output fileprefix
// TODO (5a): Copy prefix to output file name string
// TODO (5b): Concatenate input file name to output file namestring
// TODO (6): Open output file for write (exit program, iferror)
// TODO (7): Prompt and read key (don’t forget the & forthe int on scanf)
printf(“Encrypting…n”);
encipher(inFile, outFile, key);
printf(“Done.n”);
// TODO (8): Close the input and output files
return 0;
}
//———————————————————————–
// Function implementations
// TODO (9): validFileSuffix
// Returns true (1) if `str` ends in “.txt”, else false (0)
// TODO (10): shiftChar
// Returns the given character shifted by the given amount
// only change the character if `isalpha(ch)` is true
// if ch is uppercase
// if ch + shift < ‘A’, add 26 to the result
// if ch + shift > ‘Z’, subtract 26 from the result
// if ch is lowercase
// same deal as above, but with ‘a’ and ‘z’
// TODO (11): shiftStr
// Shifts all alphabetic characters by the given amount
// Mod the shift with 26 (to ensure it is in the range 0..25)
// Iterate over every character in the string:
// replace character with shifted character (callshiftChar(…))
// TODO (12): encipher
// Reads in a file, enciphers with the given key, and writes theresult
// Declare a string of size BUFFER_SIZE
// Using fgets(…), read while it does not return NULL
// Call shiftStr(…) to shift the buffer
// Use fprintf(…) to output the shifted string to the outputfile
Expert Answer
Answer to C Programming Caesar Cipher Instructions Caesar Cipher One Oldest Widely Known Encryption… . . .
OR

