Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C langauge The GNE cipher mechanism is fairly straightforward: For every n-character segment in the input, reverse the characters in that segment. For example, if

C langauge

The GNE cipher mechanism is fairly straightforward: For every n-character segment in the input, reverse the characters in that segment. For example, if n is 3, the input string "ABCABCABC" would become "CBACBACBA". If the last segment in the input string is shorter than n characters, then we leave that final segment unchanged. For example, if n is 4, the input string "0123456789" would become "3210765489". Note that the last segment with 2 characters "89" is unchanged as the segment size is 4.

Your first task is to make sure that the segment size is between 2 and 64 (inclusive). If the size is not within that valid range, print the error message "Error: segment size must be between 2 and 64." to standard error (stderr).

The next thing the program does is to take user input. We use the fgets function to get the user input and store it in the string buffer buf for you. We also do some basic input sanitizing such as removing the trailing newline ( ) from user input and checking the input length.

Your second task is to implement the function that encrypts the input string and print the result to standard output (stdout).

#include #include #include

#define BUFSZ 256

void encrypt(char *text, int stride) { }

char *get_input(const char *prompt, char *buf, size_t bufsz) { printf("%s: ", prompt); fflush(stdout);

if (!fgets(buf, bufsz, stdin)) { fprintf(stderr, "Error: failed to read input. "); exit(EXIT_FAILURE); } return buf; }

int get_stride(char *buf) { return atoi(get_input("stride", buf, BUFSZ)); }

void get_plain_text(char *buf) { get_input("input", buf, BUFSZ);

if (buf[strlen(buf) - 1] == ' ') { buf[strlen(buf) - 1] = '\0'; } else { fprintf(stderr, "Error: input string cannot exceed %d characters. ", BUFSZ); exit(EXIT_FAILURE); } }

int main() { char buf[BUFSZ];

int stride = get_stride(buf);

get_plain_text(buf);

encrypt(buf, stride);

printf()

return EXIT_SUCCESS; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions